ElasticSearch:搜索嵌套数组中的字段 [英] ElasticSearch: Searching fields in nested arrays

查看:229
本文介绍了ElasticSearch:搜索嵌套数组中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ES很新,并将其用于我的一个新项目。从开始,我有一个简单的映射给一个客户,它有一个名字和一个付款信息对象列表。如果我在SQL中这样做,那就像一个客户表,和一个1:很多关系的支付信息表。



以下是我正在尝试做的一个简单示例: https: //gist.github.com/anonymous/6109593



我希望根据嵌套数组的paymentInfos中的任何匹配找到任何客户,即找到具有billingZip 10101的paymentInfo的任何用户。此查询不返回结果,我不知道为什么。任何人都可以指出正确的方向,为什么这个查询不起作用,如果有任何改变,我可以对我的查询或映射,以使它正确地返回用户?



谢谢!

解决方案

嵌套字段应使用嵌套查询

  echo删除旧的ElasticSearch索引...
curl -XDELETE'localhost:9200 / arrtest'
echo
echo创建新的ElasticSearch索引...
curl -XPUT'localhost:9200 / arrtest /?pretty = 1'-d'{
mappings:{
cust2:{
properties:{
firstName:{
type:string,
analyzer:string_lowercase
} ,
lastName:{
type:string,
analyzer:string_lowercase
},
paymentInfos:{
properties:{
billingZip:{
t ype:string,
analyzer:string_lowercase
},
paypalEmail:{
type:string,
分析器:string_lowercase
}
},
type:嵌套
}
}
}
},

settings:{
analysis:{
analyzer:{
uax_url_email:{
filter:[standard ,smallcase],
tokenizer:uax_url_email
},

string_lowercase:{
tokenizer:keyword,
过滤器:小写
}
}
}
}
}
'
echo
echo 索引休闲完成

echo插入一条记录...
curl -XPUT'localhost:9200 / arrtest / cust2 / 1 '-d'{
firstName:john,
lastName:smith,

paymentInfos:[{
billingZip :10101,
paypalEmail:foo@bar.com
},{
billingZip:20202,
paypalEmail:foo2 @ bar2.com
}]
}
'
echo
echo刷新索引以使新记录可搜索
curl -XPOST'localhost:9200 / arrtest / _refresh'
echo
echo搜索记录...
curl -XGET'localhost:9200 / arrtest / cust2 / _search?pretty = 1'-d'{
sort:[],
查询:{
bool:{
should:[],
must_not
must:[{
nested:{
query:{
query_string:{
fields:[paymentInfos.billingZip ],
查询:10101
}
},
path:paymentInfos
}
}]
}
},
facets:{},
from:0,
size:25
}'
echo


I'm fairly new to ES and am using it for a new project of mine. Starting off, I have a simple mapping for a customer, which has a first and last name, and a list of payment information objects. If I were doing this in SQL, it would be something like a customer table, and a payment info table with a 1:many relationship.

Here's a simple example of what I'm trying to do: https://gist.github.com/anonymous/6109593

I'm hoping to find any customer based on any match in the nested array of paymentInfos, i.e. finding any users who've had a paymentInfo with billingZip 10101. This query returns no results, and I'm not sure why. Can anyone point me in the right direction as to why this query doesn't work, and if there are any changes I can make to either my query or mapping to have it return the user properly?

Thanks!

解决方案

Nested fields should be searched using nested query:

echo "Deleting old ElasticSearch index..."
curl -XDELETE 'localhost:9200/arrtest'
echo
echo "Creating new ElasticSearch index..."
curl -XPUT 'localhost:9200/arrtest/?pretty=1' -d '{
   "mappings" : {
      "cust2" : {
         "properties" : {
            "firstName" : {
               "type" : "string",
               "analyzer" : "string_lowercase"
            },
            "lastName" : {
               "type" : "string",
               "analyzer" : "string_lowercase"
            },
            "paymentInfos": {
                "properties": {
                    "billingZip": {
                        "type": "string",
                        "analyzer": "string_lowercase"
                    },
                    "paypalEmail": {
                        "type": "string",
                        "analyzer": "string_lowercase"
                    }
                },
                "type": "nested"
            }
         }
      }
   },

   "settings" : {
      "analysis" : {
         "analyzer" : {
            "uax_url_email" : {
               "filter" : [ "standard", "lowercase" ],
               "tokenizer" : "uax_url_email"
            },

            "string_lowercase": {
                "tokenizer" : "keyword",
                "filter" : "lowercase"
            }
         }
      }
   }
}
'
echo
echo "Index recreation finished"

echo "Inserting one record..."
curl -XPUT 'localhost:9200/arrtest/cust2/1' -d '{
    "firstName": "john",
    "lastName": "smith",

    "paymentInfos": [{
        "billingZip": "10101",
        "paypalEmail": "foo@bar.com"
    }, {
        "billingZip": "20202",
        "paypalEmail": "foo2@bar2.com"
    }]
}
'
echo
echo "Refreshing index to make new records searchable"
curl -XPOST 'localhost:9200/arrtest/_refresh' 
echo
echo "Searching for record..."
curl -XGET 'localhost:9200/arrtest/cust2/_search?pretty=1' -d '{
    "sort": [],
    "query": {
        "bool": {
            "should": [],
            "must_not": [],
            "must": [{
                "nested": {
                    "query": {
                        "query_string": {
                            "fields": ["paymentInfos.billingZip"],
                            "query": "10101"
                        }
                    },
                    "path": "paymentInfos"
                }
            }]
        }
    },
    "facets": {},
    "from": 0,
    "size": 25
}'
echo

这篇关于ElasticSearch:搜索嵌套数组中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆