Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 259
ehsan shirzadi Avatar asked Oct 03 '14 20:10

ehsan shirzadi


1 Answers

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" }}} 
      ]
    }
  }
}
like image 117
Dan M Avatar answered Oct 21 '22 07:10

Dan M