一尘不染

ElasticSearch:在嵌套数组中搜索字段

elasticsearch

我对ES还是相当陌生,并正在将其用于我的新项目。首先,我为客户提供了一个简单的映射,其中包含名字和姓氏以及付款信息对象列表。如果我在SQL中执行此操作,那将类似于客户表和具有1:许多关系的付款信息表。

这是我要执行的操作的一个简单示例:https :
//gist.github.com/anonymous/6109593

我希望根据payInfos嵌套数组中的任何匹配项找到任何客户,即找到所有使用billingZip
10101拥有PaymentInfo的用户。此查询没有返回结果,我不确定为什么。谁能为我指出正确的方向,为什么这个查询不起作用,如果我可以对查询或映射进行任何更改以使其正确返回用户?

谢谢!


阅读 763

收藏
2020-06-22

共1个答案

一尘不染

嵌套字段应使用嵌套查询进行搜索:

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
2020-06-22