ElasticSearch过滤器没有返回结果...语法问题?

时间:2013-12-22 11:30:09

标签: grails lucene elasticsearch filtering

我有弹性搜索文档,如下所示

{
_index: ecw
_type: grails.gorm.tests.Person
_id: Nb0tHzNDRtq3-rGSO3yVTw
_score: 1
_source: {
firstName: Bart
lastName: Simpson
age: 0
pets: [ ]
}
}
{
_index: ecw
_type: grails.gorm.tests.Person
_id: RxD7mGsvR3i5LT52MAdSLA
_score: 1
_source: {
firstName: Bob
lastName: Builder
age: 0
pets: [ ]
}
}
{
_index: ecw
_type: grails.gorm.tests.Person
_id: rJMEbBqeRcqx3K0HtfGrLg
_score: 1
_source: {
firstName: Bart
lastName: Simpson
age: 9
pets: [ ]
}
}

但是当我在localhost上运行[post]以下过滤器:9200 / ecw / grails.gorm.tests.Person / _search使用head时它什么都不返回。有什么想法吗?

{
      "filter": {
        "and": {
          "filters": [
            {
              "term": {
                "lastName": "Simpson"
              }
            }
          ]
        }
      }
    }

1 个答案:

答案 0 :(得分:2)

首先,这不是使用and的正确方法。它需要一个直接过滤器列表,即{"and": [ {"term": ...} ]}。 (另请参阅http://www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

此外,term过滤器不进行任何文本处理/分析。对文档编制索引时,Simpson可能已小写为simpson(等等)。

因此,您需要对{ "term": { "lastName": "Simpson" } }进行过滤。

最后,如果您想同时使用构面和匹配,则只应在搜索对象上使用filter,但不希望过滤器影响构面。在所有其他情况下,您希望使用filtered - 查询。

总而言之,这就是你最终的目标:

{
    "query": {
        "filtered": {
            "filter": {
                "term": {
                    "lastName": "simpson"
                }
            }
        }
    }
}

这是一个可运行的示例:https://www.found.no/play/gist/d608591254288783cd0d

相关问题