弹性搜索范围和术语过滤器的组合

时间:2014-10-03 20:07:05

标签: elasticsearch

我想要一个过滤器来返回包含're_max' > 100language_id = 28的文档。我是这样做的:

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

但似乎不正确。我应该怎么纠正呢? 这是错误:

{
  "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
}

4 个答案:

答案 0 :(得分:14)

“过滤”查询在2.0中已弃用,在5.0中已弃用removed

可以使用Elasticsearch 5和6 filter

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

答案 1 :(得分:5)

注意: 过滤的查询已被最近版本的ElasticSearch中的 bool 查询所取代。有关详细信息,请参阅the docs

您不是第一个被过滤查询混淆的人:)

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

更新:如果要将两个条件用作组合过滤器,可以将它们与bool或和filter连接,并完全省略查询部分。以下是and的示例:

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

答案 2 :(得分:0)

以下查询正在使用范围过滤器和术语过滤器。

{
"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"
        ]
      }
    }
  ]
  }
 }
}

答案 3 :(得分:0)

我尝试过Dan M&'s的查询,但如果没有“必须”的话,它似乎没有用。 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

以下对我有用:

  

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