Elasticsearch [filtered]查询不支持[建议]

时间:2016-05-15 22:46:36

标签: elasticsearch filter geo

我想允许我们的用户按文字和地理位置搜索对象。通过文本搜索虽然我希望它建议使用正常查询和完成建议的条款。当我尝试将建议者添加到我的过滤查询时,我收到错误。有什么建议吗?

我的映射:

curl -XPUT http://localhost:9200/objects/object/_mapping -d '{
  "object": {
    "properties": {
      "objectDescription": {
        "type": "string"
      },
      "suggest": {
        "type": "completion",
        "analyzer": "standard",
        "search_analyzer": "standard",
        "payloads": true
      }
    }
  }
}'

curl -X PUT http://localhost:9200/objects/object/_mapping -d '{
    "object":{"properties":{"location":{"type":"geo_point"}}}
}'

我的搜索:

{
  "query": {
    "filtered": {
      "query": {
        "fuzzy": {
          "objectDescription": {
            "value": "burger"
          }
        }
      },
      "filter": {
        "or": [
          {
            "geo_distance": {
              "distance": "15mi",
              "location": {
                "lat": 33.4255104,
                "lon": -111.94000540000002
              }
            }
          },
          {
            "bool": {
              "must": [
                {
                  "term": {
                    "administrative_area_level_1": "AZ"
                  }
                },
                {
                  "term": {
                    "addressType": "administrative_area_level_1"
                  }
                }
              ]
            }
          }
        ]
      }
    }
  }
}

当我在没有过滤器的情况下添加与查询相同级别的建议时,它会忽略我的过滤器。它会在搜索时找到对象,而不是在lat lng附近。

{
    "query":{
        "filtered":{
            "query":{
                "fuzzy":{
                    "objectDescription":{"value":"bears"}
                }
            },
            "filter":{
                "bool":{
                    "should":[
                        {
                            "geo_distance":{
                                "distance":"15mi",
                                "location":{
                                    "lat":43.6187102,
                                    "lon":-116.21460680000001
                                }
                            }
                        },
                        {
                            "bool":{
                                "must":[
                                    {
                                        "term":{"administrative_area_level_1":"ID"}
                                },{
                                        "term":{"addressType":"administrative_area_level_1"}
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    },
    "suggest":{
        "object-suggest":{
            "text":"bears",
            "completion":{
                "field":"suggest",
                "fuzzy":{"fuzziness":2}
            }
        }
    }
}

当我将身体的建议部分放在查询的位置时,我得到了这个错误。

{
    "query":{
        "filtered":{
            "suggest":{
                "object-suggest":{
                    "text":"bears",
                    "completion":{
                        "field":"suggest",
                        "fuzzy":{"fuzziness":2}
                    }
                }
            },
            "filter":{
                "bool":{
                    "should":[
                        {
                            "geo_distance":{
                                "distance":"15mi",
                                "location":{
                                    "lat":43.6187102,
                                    "lon":-116.21460680000001
                                }
                            }
                        },
                        {
                            "bool":{
                                "must":[
                                    {
                                        "term":{"administrative_area_level_1":"ID"}
                                },{
                                        "term":{"addressType":"administrative_area_level_1"}
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    }
}

所以我的问题是我没有在elastic.co或stackoverflow上找到一个例子,它具有地理位置搜索和完成建议器的组合。

1 个答案:

答案 0 :(得分:1)

试试这样。 { "query": { "filtered": { "filter": { "bool": { "should": [ { "geo_distance": { "distance": "15mi", "location": { "lat": 43.6187102, "lon": -116.21460680000001 } } }, { "bool": { "must": [ { "term": { "administrative_area_level_1": "ID" } }, { "term": { "addressType": "administrative_area_level_1" } } ] } } ] } } } }, "suggest": { "object-suggest": { "text": "bears", "completion": { "field": "suggest", "fuzzy": { "fuzziness": 2 } } } } } 块自身位于顶层:

info.plist

<强>更新

对于您的确切用例,我认为您应该将context suggester与地理位置上下文一起使用,而不是使用简单的完成建议器。为了达到你的期望,这肯定是你所需要的。