Elasticsearch filtered search for sub-object fields

How to perform a filtered search in ElasticSearch in a sub-object field? For example, my document has a field idand data, which is a JSON array, as the data was stored in my database:

{_id: 000, merchant: "merchant_1", email: "hello@email.com"}

My search query:

"query": {
    "filtered": {
        "filter": { "term": { "data.merchant": "merchant_1"} },
        "query": {
            "query_string": {"query": "hello"} }
        }
    }
}

returns nothing, but executing a query using query_string helloreturns the correct rows. Change "data.merchant"=> "merchant"also does not change anything.

Am I doing something wrong here?

Update: . In the end, I just used a logical query that worked.

+5
source share
1 answer
"query": {
    "filtered": {
        "filter": { "term": { "data.merchant": "merchant_1"} },
        "query": {
            "query_string": {
                "default_field": "_all",
                "query": "hello"
            }
        }
    }
}

This should work for your case.

0
source

All Articles