ehsan shirzadi
ehsan shirzadi

Reputation: 4859

Elastic Search combination of Range and Term filters

I want to have a filter to return documents with 're_max' > 100 and language_id = 28. I did it this way:

{
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "re_max": {
            "gt": 100
          }
        },
        "term": {
          "language_id": 28
        }
      }
    }
  }
}

But seems like it's not correct. how should I correct it?
This is the error:

{
  "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; 
            shardFailures {[WkgqspjtQzmPkW-YdvE1Qg][rep][0]: SearchParseException[[rep][0]: 
              from[-1],size[-1]: 
              Parse Failure [Failed to parse source [
                {
                  "query": {
                    "filtered": {
                      "filter": {
                        "or": [
                          "range":
                          {
                            "re_max": {
                              "gt": 100
                            }
                          },
                          "term":
                          {
                            "language_id": 28
                          }
                        ]
                      }
                    }
                  }
                }
              ]]]; 
            nested: QueryParsingException[[rep] Failed to parse]; 
            nested: JsonParseException[Unexpected character (':' (code 58)): 
              was expecting comma to separate ARRAY entries\n at [Source: [B@3d44a542; line: 6, column: 19]]; }{[WkgqspjtQzmPkW-YdvE1Qg][rep][4]: 
              SearchParseException[[rep][4]: from[-1],size[-1]: 
                Parse Failure [Failed to parse source [
                  {
                    "query": {
                      "filtered": {
                        "filter": {
                          "or": [
                            "range":
                            {
                              "re_max": {
                                "gt": 100
                              }
                            },
                            "term":
                            {
                              "language_id": 28
                            }
                          ]
                        }
                      }
                    }
                  }
                ]]]; 
              nested: QueryParsingException[[rep] Failed to parse]; 
              nested: JsonParseException[Unexpected character (':' (code 58)): 
                was expecting comma to separate ARRAY entries\n at [Source: [B@3d44a542; line: 6, column: 19]]; }{[WkgqspjtQzmPkW-YdvE1Qg][rep][3]: 
                SearchParseException[[rep][3]: from[-1],size[-1]: 
                  Parse Failure [Failed to parse source [
                    {
                      "query": {
                        "filtered": {
                          "filter": {
                            "or": [
                              "range":
                              {
                                "re_max": {
                                  "gt": 100
                                }
                              },
                              "term":
                              {
                                "language_id": 28
                              }
                            ]
                          }
                        }
                      }
                    }
                  ]]]; 
                nested: QueryParsingException[[rep] Failed to parse]; 
                nested: JsonParseException[Unexpected character (':' (code 58)): 
                  was expecting comma to separate ARRAY entries\n at [Source: [B@3d44a542; line: 6, column: 19]]; }{[WkgqspjtQzmPkW-YdvE1Qg][rep][2]: 
                  SearchParseException[[rep][2]: from[-1],size[-1]: 
                    Parse Failure [Failed to parse source [
                      {
                        "query": {
                          "filtered": {
                            "filter": {
                              "or": [
                                "range":
                                {
                                  "re_max": {
                                    "gt": 100
                                  }
                                },
                                "term":
                                {
                                  "language_id": 28
                                }
                              ]
                            }
                          }
                        }
                      }
                    ]]]; 
                  nested: QueryParsingException[[rep] Failed to parse]; 
                  nested: JsonParseException[Unexpected character (':' (code 58)): 
                    was expecting comma to separate ARRAY entries\n at [Source: [B@3d44a542; line: 6, column: 19]]; }{[WkgqspjtQzmPkW-YdvE1Qg][rep][1]: 
                    SearchParseException[[rep][1]: from[-1],size[-1]: 
                      Parse Failure [Failed to parse source [
                        {
                          "query": {
                            "filtered": {
                              "filter": {
                                "or": [
                                  "range":
                                  {
                                    "re_max": {
                                      "gt": 100
                                    }
                                  },
                                  "term":
                                  {
                                    "language_id": 28
                                  }
                                ]
                              }
                            }
                          }
                        }
                      ]]]; 
                    nested: QueryParsingException[[rep] Failed to parse]; 
                    nested: JsonParseException[Unexpected character (':' (code 58)): 
                      was expecting comma to separate ARRAY entries\n at [Source: [B@3d44a542; line: 6, column: 19]]; }]",
  "status": 400
}

Upvotes: 15

Views: 26482

Answers (4)

Ashalynd
Ashalynd

Reputation: 12563

Note: The filtered query has been replaced by the bool query in recent versions of ElasticSearch. See the docs for more info.

You are not the first person confused by the filtered query :)

 {
  "query": {
    "filtered": {
      "query": {
        "term": {
          "language_id": 28
        }
      },
      "filter": {
        "range": {
          "re_max": {
          "gt": 100
          }
        }
      }
    }
  }
}

Update: if you want to use both conditions as a combined filter, you can join them with bool or and filter, and omit the query part altogether. Example with and follows:

{
    "query":{
        "filtered":{
            "filter":{
                "and":[
                    {
                        "range":{
                            "re_max":{
                                "gt":100
                            }
                        }
                    },
                    {
                        "term":{
                            "language_id":28
                        }
                    }
                ]
            }
        }
    }
}

Upvotes: 7

Epee
Epee

Reputation: 1

I tried Dan M's query but it did not seem to work without a 'must'. https://discuss.elastic.co/t/after-upgrade-elasticsearch-version-getting-error-no-query-registered-for-filter-line-1-col-20-type-parsing-exception-reason-no-query-registered-for-filter-line-1-col-20-status-400/109571

The following worked for me:

    {
      "query": { 
        "bool": { 
          "must": { "term":  { "clientid": 3066  }},
                    "filter": {
                               "range" : { 
                                          "date" : { 
                                                    "gte" : "2018-02-14T00:00:00",  "lte" : "2018-02-21T13:00:00" }}}}
    }
}

Upvotes: -1

Dan M
Dan M

Reputation: 1272

The "filtered" query was deprecated in 2.0 and removed in 5.0

Elasticsearch 5 and 6 filter can be used:

{
  "query": { 
    "bool": { 
      "filter": [ 
        { "term":  { "language_id": 28  }},
        { "term":  { "some_other_term": "some string value"}},
        { "range": { "created_at_timestamp": { "gt": "2015-01-01" }}} 
      ]
    }
  }
}

Upvotes: 39

Nikunj B. Balar
Nikunj B. Balar

Reputation: 298

Below query is working with range filter as well as term filter.

{
"size": 1,
"from": 0,
"sort": [
{
  "inDeviceDateTime": {
    "order": "desc"
  }
}
],
"query": {
"bool": {
  "must": {
    "range": {
      "inDeviceDateTime": {
        "gte": 1500316199000,
        "lte": 1500336000000
      }
    }
  },
  "must": {
    "term": {
      "inType": 3
    }
  },
  "must": [
    {
      "multi_match": {
        "query": "Mom",
        "fields": [
          "stFrom",
          "stTo"
        ]
      }
    }
  ]
  }
 }
}

Upvotes: 1

Related Questions