2015-02-07 114 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" 
      } 
     ] 
     } 
    } 
}