0

My structure:

  • Person: type object.

  • Person.Cars: type nested.

  • Person.Cars.Radios: type nested.

I want to write a query that should find all person with:

  • Car X
  • Car year from 40 to 100
  • Radio in the car Y
 {
      "query": {
        "nested": {
          "path": "person.cars",
          "query": {
            "bool": {
              "filter": [
                {
                  "match": {
                    "person.cars.id": {
                      "query": "X"
                    }
                  }
                },
                {
                  "range": {
                    "person.cars.year": {
                      "from": 40,
                      "to": 100
                    }
                  }
                }
              ]
            },
            "nested": {
              "path": "person.cars.radios",
              "query": {
                "bool": {
                  "filter": [
                    {
                      "match": {
                        "person.cars.radios.id": {
                          "query": "Y"
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }

My response:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
                "line": 26,
                "col": 5
            }
        ],
        "type": "parsing_exception",
        "reason": "[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
        "line": 26,
        "col": 5
    },
    "status": 400
}

How to write query like this? I know that I have to move my second nested part, but I don't know how.

2 Answers 2

1

I suggest you to add, your second nested query as a part of your first bool-filter.

 {
  "query": {
    "nested": {
      "path": "person.cars",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "person.cars.id": {
                  "query": "X"
                }
              }
            },
            {
              "range": {
                "person.cars.year": {
                  "from": 40,
                  "to": 100
                }
              }
            },
            {
              "nested": {
                "path": "person.cars.radios",
                "query": {
                  "bool": {
                    "filter": [
                      {
                        "match": {
                          "person.cars.radios.id": {
                            "query": "Y"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

{ "error": { "root_cause": [ { "type": "parsing_exception", "reason": "no [query] registered for [query]", "line": 24, "col": 17 } ], "type": "parsing_exception", "reason": "no [query] registered for [query]", "line": 24, "col": 17 }, "status": 400 }
My version: 6.1.1
For version 6.2.4 the same problem. This is invalid query. :(
@AdrianKlimczak Had no time until now to fix the query, glad to see that you've found the solution.
Can you edit your answer? After that I will accept your answer. :)
|
0

@jordivador you were so close.:) Just remove query key after range filter.

Good idea: use Java API - to check how to write a query.

{
  "query": {
    "nested": {
      "path": "person.cars",
      "query": {
        "bool": {
          "filter": [
            {
              "match": {
                "person.cars.id": {
                  "query": "X"
                }
              }
            },
            {
              "range": {
                "person.cars.year": {
                  "from": 40,
                  "to": 100
                }
              }
            },
            {
              "nested": {
                "path": "person.cars.radios",
                "query": {
                  "bool": {
                    "filter": [
                      {
                        "match": {
                          "person.cars.radios.id": {
                            "query": "Y"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.