2015-10-14 37 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 –