ElasticSearch: search for fields in nested arrays

I am new to ES and use it for my new project. To begin with, I have a simple mapping for a client who has a first and last name, as well as a list of payment information objects. If I did this in SQL, it would be like a customer table and a payment information table with a ratio of 1: many.

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

I hope to find any client based on any match in the paymentInfos nested array, that is, to find any users who had payment information with billingZip 10101. This query does not return any results, and I'm not sure why. Can someone point me in the right direction, why this request does not work, and if there are any changes that I can make for my request or collation so that it returns the user correctly?

Thanks!

+6
source share
1 answer

Nested fields should be searched using nested queries :

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 
+9
source

Source: https://habr.com/ru/post/950591/


All Articles