Question

I have an elasticsearch index for storing information about people. To find specific persons I have some queries, each of them works alone but when I combine them using Bool Query I get an error.

One of the queries is a fuzzy search for the name

{
  "query": {
    "fuzzy_like_this": {
      "fields": [
        "firstname",
        "lastname"
      ],
      "like_text": "Peter"
    }
  }
}

Another query is for searching people who are born in a specific date range

{
  "query": {
    "range": {
      "birthdate": {
        "from": "1988-12-30",
        "to": "1993-12-30"
      }
    }
  }
}

Now I want to combine these two queries. My bool query:

{
  "query": {
    "bool": {
      "must": [
        {
          "query": {
            "fuzzy_like_this": {
              "fields": [
                "firstname",
                "lastname"
              ],
              "like_text": "Peter"
            }
          }
        },
        {
          "query": {
            "range": {
              "birthdate": {
                "from": "1988-12-30",
                "to": "1993-12-30"
              }
            }
          }
        }
      ]
    }
  }
}

Although both queries work fine when I use them separately, when combining them I get an error. There are people in my index whose firstname is Peter AND are born in this date range, but even if there were no people found I should get 0 results instead of an error.

The error says: "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; nested: QueryParsingException[[indexname] No query registered for [query]]

Is combining queries the way I want to not possible with a bool query or did I just use the wrong syntax?

Was it helpful?

Solution

I think that you have a syntax error, the keyword query is not needed for queries that belong to must. In other words, it should be as follows:

{
  "query": {
    "bool": {
      "must": [
        {
          "fuzzy_like_this": {
            "fields": [
              "firstname",
              "lastname"
            ],
            "like_text": "Peter"
          }
        },
        {
          "range": {
            "birthdate": {
              "from": "1988-12-30",
              "to": "1993-12-30"
            }
          }
        }
      ]
    }
  }
}

More info about boolean queries here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top