Elasticsearch-dsl-py: Nested query on Array of Object

Created on 23 Aug 2018  路  4Comments  路  Source: elastic/elasticsearch-dsl-py

Hi @HonzaKral,

I have below data and I want to search doc based on name field name and I have trying below lines of code:

q = Q("exists", field="dealers")
q &= Q("nested", path="dealers", query=Q("bool", filter=[Q("match", dealers__name="A1")]))

response = Search().query(q)

Model:

class MyDoc:
    vendor = Keyword(required=True)
    model = Keyword(required=True)
    dealers = Nested(
        multi=True,
        properties={
            "name": Keyword(),
            "items": Nested(multi=True, properties={
                "price": Double()
            })
        })

    class Index:
        name = "mytest"
        doc_type = "test"

Data:

{
  "vendor": "ABC",
  "model": "XYZ",
  "dealers": [
    {
      "name": "A1",
      "items": [
        {
          "price": 25.456486237137668
        }
      ]
    },
    ...
  ]
}
{
  "vendor": "ABC",
  "model": "XYZ",
  "dealers": [
    {
      "name": "A2",
      "items": [
        {
          "price": 63.41575806397323
        }
      ]
    },
    ...
  ]
}

But it throws an error:

lasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query:...

How can I achieve this?

Most helpful comment

I think I got it, I need to use direct query:

q = Q("match", dealers__name="A1")
response = Search().query(q)

Thanks

All 4 comments

There is no need to verify if a field exists when you want to query it - if a document has no value for the given field it will never match a query against it. In your case you should just have the nested query.

The error you are seeing is caused by dealers not being a real field in the index - nested fields, just like object fields are not indexed themselves and so cannot be queried, only their subfields (dealers.name for example) can be.

Hope this helps!

@HonzaKral Thanks for response, then how query will be look like? below is entire error:

elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to create query: {\n  "nested" : {\n    "query" : {\n      "bool" : {\n        "filter" : [\n          {\n            "match" : {\n              "dealers.name" : {\n                "query" : "A1",\n                "operator" : "OR",\n                "prefix_length" : 0,\n                "max_expansions" : 50,\n                "fuzzy_transpositions" : true,\n                "lenient" : false,\n                "zero_terms_query" : "NONE",\n              "auto_generate_synonyms_phrase_query" : true,\n                "boost" : 1.0\n              }\n            }\n          }\n        ],\n        "adjust_pure_negative" : true,\n        "boost" : 1.0\n      }\n    },\n    "path" : "dealers",\n    "ignore_unmapped" : false,\n    "score_mode" : "avg",\n    "boost" : 1.0\n  }\n}'

I think I got it, I need to use direct query:

q = Q("match", dealers__name="A1")
response = Search().query(q)

Thanks

I think I got it, I need to use direct query:

q = Q("match", dealers__name="A1")
response = Search().query(q)

Thanks

This worked.
Been looking for a solution and this worked perfectly

Was this page helpful?
0 / 5 - 0 ratings