2013-07-10 66 views
0

我在弹性搜索中有一个奇怪的行为。我开始使用过滤器之前,我有一个查询: ElasticSearch过滤器对分类的影响

查询不带过滤器

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "_facility": { 
       "query": "error", 
       "operator": "AND" 
      } 
      } 
     }, 
     { 
      "match": { 
      "_application": { 
       "query": "live", 
       "operator": "AND" 
      } 
      } 
     } 
     ], 
     "must_not": [], 
     "should": [] 
    } 
    }, 
    "from": 0, 
    "size": 10, 
    "sort": [ 
    { 
     "created_at": { 
     "sort_mode": null, 
     "order": "desc", 
     "missing": null, 
     "ignore_unmapped": null 
     } 
    }, 
    { 
     "_score": { 
     "sort_mode": null, 
     "order": null, 
     "missing": null, 
     "ignore_unmapped": null 
     } 
    } 
    ] 
} 

然后我不得不加滤网

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "match": { 
      "_facility": { 
       "query": "error", 
       "operator": "AND" 
      } 
      } 
     }, 
     { 
      "match": { 
      "_application": { 
       "query": "live", 
       "operator": "AND" 
      } 
      } 
     } 
     ], 
     "must_not": [], 
     "should": [] 
    } 
    }, 
    "filter": { 
    "and": { 
     "filters": [ 
     { 
      "range": { 
      "created_at": { 
       "from": 1373320800, 
       "to": 1373493599, 
       "include_lower": true, 
       "include_upper": true 
      }, 
      "_cache": true 
      } 
     } 
     ] 
    }, 
    "_cache": false 
    }, 
    "from": 0, 
    "size": 10, 
    "sort": [ 
    { 
     "created_at": { 
     "sort_mode": null, 
     "order": "desc", 
     "missing": null, 
     "ignore_unmapped": null 
     } 
    }, 
    { 
     "_score": { 
     "sort_mode": null, 
     "order": null, 
     "missing": null, 
     "ignore_unmapped": null 
     } 
    } 
    ] 
} 

和我有未来的问题:

1 )结果没有正确地按照created_at排序,看起来像洗牌数据

2)大小 - 不考虑任何自定义值不同于10(假设我想显示20 [或5]记录,但我有10)

感谢您的帮助。可能我在弹性搜索概念中缺少一些东西。

+0

您是否尝试使用FilteredQuery? http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query/ – Damien

+0

我试过了,但ElasticSearch抛出了一个解析错误。你能给我正确的json吗? –

+0

感谢您的提示。问题出在'_cache'错误的位置,因为@imotov提到 –

回答

1

问题是在and过滤器实际上是在过滤器之外。它应该更深一层:

"filter": { 
    "and": { 
    "filters": [{ 
     "range": { 
     "created_at": { 
      "from": 1373320800, 
      "to": 1373493599, 
      "include_lower": true, 
      "include_upper": true 
     }, 
     "_cache": true 
     } 
    }], 
    "_cache": false 
    } 
} 

它抛出Elasticsearch解析器,它开始忽略其余的查询。顺便说一句,这些_cache声明是无用的,因为它们只是确保默认设置。而且@Damien说,整个请求会更好,因为filtered查询:

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "bool": { 
      "must": [{ 
      "match": { 
       "_facility": { 
       "query": "error", 
       "operator": "AND" 
       } 
      } 
      }, { 
      "match": { 
       "_application": { 
       "query": "live", 
       "operator": "AND" 
       } 
      } 
      }] 
     } 
     }, 
     "filter": { 
     "and": { 
      "filters": [{ 
      "range": { 
       "created_at": { 
       "from": 1373320800, 
       "to": 1373493599, 
       "include_lower": true, 
       "include_upper": true 
       }, 
       "_cache": true 
      } 
      }, { 
      "match_all": {} 
      }], 
      "_cache": false 
     } 
     } 
    } 

    }, 
    "from": 0, 
    "size": 10, 
    "sort": [{ 
    "created_at": { 
     "order": "desc" 
    } 
    }, { 
    "_score": {} 
    }] 
} 
+0

谢谢!在我的情况下,_cache参数是问题。我不需要把它放到'过滤'区块。 –