2015-10-14 39 views
1

我正在使用Elasticsearch查询文档类型,该文档类型具有可选位置字段。搜索时,如果该字段不存在,应返回这些结果,并对结果进行过滤。在Elasticsearch可选字段上过滤

这似乎是OR过滤器Elasticsearch不会短路,因为这样:

"query": { 
    "filtered": { 
     "query": { 
      "match_phrase_prefix": { 
       "display_name": "SearchQuery" 
      } 
     }, 
    "filter": { 
     "or": [ 
     { 
      "missing": { 
       "field": "location" 
      } 
     }, 
     { 
      "geo_distance" : { 
       "distance" : "20mi", 
       "location" : { 
        "lat" : 33.47, 
        "lon" : -112.07 
       } 
      } 
     } 
    ] 
} 

无法与“未能找到geo_point场[位置]”。

是否有任何方法在ES中执行此操作(或沿着相同的静脉)?

回答

0

对于具有相同问题的人,我有种就在其砍死。对于任何缺少“位置”的文档,我添加了一个纬度/经度为0/0的文档。然后我改变我的查询是:

"filter": { 
    "or": [ 
     { 
      "geo_distance": { 
       "distance": "0.1mi", 
       "location": { 
        "lat": 0, 
        "lon": 0 
       } 
      } 
     }, 
     { 
      "geo_distance": { 
       "distance": "30mi", 
       "location": { 
        "lat": [lat variable], 
        "lon": [lon variable] 
       } 
      } 
     } 
    ] 
} 
0

我不知道你为什么不工作,但我过去使用bool filter取得了巨大成功。 should选项本质上是一个or并确保至少有一个为真。如果它仍然不起作用,请尝试并对我的答案发表评论。同时仔细检查我正确复制您的查询词:)

{ 
    "filtered" : { 
     "query" : { 
      "match_phrase_prefix": { 
       "display_name": "SearchQuery" 
      } 
     }, 
     "filter" : { 
      "bool" : { 
       "should" : [ 
        { 
         "missing": { "field": "location" } 
        }, 
        { 
         "geo_distance" : { 
          "distance" : "20mi", 
          "location" : { 
           "lat" : 33.47, 
           "lon" : -112.07 
          } 
         } 
        } 
       ] 
      } 
     } 
    } 
} 
+0

谢谢,但仍然给我同样的错误。看起来它试图执行第二个条款,即使第一个条款匹配。 – Justin

+0

https://www.elastic.co/guide/en/elasticsearch/guide/current/_dealing_with_null_values.html#_missing_query –