Search within array object

2019-09-06 03:24发布

I have a the following json object --

{
  "Title": "Terminator,
  "Purchases": [
     {"Country": "US", "Site": "iTunes"},
     {"Country": "FR", "Site": "Google"}
  ]
}

Given the above object, here is how the search results show yield:

"Titles on iTunes in US" ==> YES, show "Terminator"
"Titles on Google in FR" ==> YES, show "Terminator"
"Titles on iTunes in FR" ==> NO

However, if I just AND the query, to get Titles with Purchase.Country="FR" and Titles with Purchase.Site="iTunes", it would erroneously show the above result, since both conditions are met. However, I want to restrict that facet to within the purchase item. The equivalent in python code would be:

for purchase in item['Purchases']:
    if purchase['Country'] == "FR" and purchase['Site'] == "iTunes":
        return True

Currently it works like this:

for purchase in item['Purchases']:
    if purchase['Country'] == "FR":
        has_fr = True
    if purchase['Site'] == "iTunes":
        has_itunes = True
if has_itunes and has_fr: return True

How would this be done in ElasticSearch?

1条回答
smile是对你的礼貌
2楼-- · 2019-09-06 03:38

First, you need to index the "Purchases" field as a nested field, by defining the mapping of your object type like this:

{
    "properties" : {
        "Purchases" : {
            "type" : "nested",
            "properties": {
                "Country" : {"type": "string" },
                "Site"  : {"type": "string" }
            }
        }
    }
}

Only then will ElasticSearch keep the association between the individual countries and the individual sites, as described here.

Next, you should use a nested query, such as this one:

{ "query": 
    { "nested" : {
            "path" : "Purchases",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                        {
                            "match" : {"Purchases.Country" : "US"}
                        },
                        {
                            "match" : {"Purchases.Site" : "iTunes"}
                        }
                    ]
                }
            }
        }
    }
}

This will return your object if the query combines "US" and "iTunes", but not if it combines "US" and "Google". The details are described here.

查看更多
登录 后发表回答