2015-02-07 115 views
4

嘗試創建一個搜索,它將帶回距離某個地理點約500m的位置結果。帶過濾器的ElasticSearch function_score查詢

我需要根據源位置是否爲空來過濾來自此搜索的結果。

我試過這樣的事情:

"filtered" : { 
    "filter": { 
     "missing" : { "field" : "location" } 
    } 
} 

這是搜索JSON我:

{"query": { 
     "function_score": { 
      "query": { 
       "bool": { 
        "must": [ 
         { 
          "match": {"fieldA": "value"} 
         }, 
         { 
          "match": { "fieldB": "value"} 
         } 
        ] 
       } 
      }, 
      "functions": [{ 
        "gauss": { 
         "location": { 
          "origin": "'.$lat.','.$lon.'", 
          "scale": "500m", 
          "offset": "0km", 
          "decay": 0.33 
         } 
        } 
       }] 
     } 
    } 
} 

我試圖把過濾器在不同的地方查詢,但它並沒有爲工作我,所以我知道我正在做一些根本上錯誤的查詢結構。將來我想添加更多的評分邏輯和其他過濾器,但是找不到這樣的查詢的一個好例子。

我應該怎麼做才能使它工作?

回答

14

您可以使用帶有geo_distance的已過濾查詢先篩選出結果。您還可以使用function_score中的「weight」來顯式提高「匹配」查詢的距離:

{ 
    "query": { 
     "function_score": { 
     "query": { 
      "filtered": { 
       "query": { 
        "bool": { 
        "must": [ 
         { 
          "match": { 
           "fieldA": "value" 
          } 
         }, 
         { 
          "match": { 
           "fieldB": "value" 
          } 
         } 
        ] 
        } 
       }, 
       "filter": { 
        "geo_distance" : { 
         "distance" : "500m", 
         "location" : "'.$lat.','.$lon.'" 
        } 
       } 
      } 
     }, 
     "functions": [ 
      { 
       "gauss": { 
        "location": { 
        "origin": "'.$lat.','.$lon.'", 
        "scale": "500m", 
        "offset": "0km", 
        "decay": 0.33 
        } 
       }, 
       "weight": "3" 
      } 
     ] 
     } 
    } 
}