2017-02-17 45 views
0

我需要做的是按位置增加文檔(越接近越好)。 locations是嵌套類型。function_score:將缺失的字段視爲完美匹配

可以正常工作如果文檔中缺少locations,則Elasticsearch不返回文檔。如果缺少字段,Elasticsearch應將文檔視爲完美匹配。任何想法如何實現這一目標?

我的查詢:

{ 
    "sort": [ 
     { 
     "_score": "desc" 
     } 
    ], 
    "query": { 
     "function_score": { 
     "query": { 
      "bool": { 
       "must_not": [], 
       "should": [ 
        { 
        "nested": { 
         "path": "locations", 
         "query": { 
          "function_score": { 
           "score_mode": "sum", 
           "functions": [ 
           { 
            "gauss": { 
             "locations.coordinates": { 
              "origin": { 
              "lat": "50.1078852", 
              "lon": "15.0385376" 
              }, 
              "scale": "100km", 
              "offset": "20km", 
              "decay": "0.5" 
             } 
            } 
           } 
           ] 
          } 
         } 
        } 
        } 
       ] 
      } 
     } 
     } 
    } 
} 

BTW:我使用Elasticsearch 5.0

回答

0

一個更function添加與高增壓(如1000)的功能部分缺失locations,是這樣的:

{ 
    "query": { 
     "bool": { 
      "must_not": { 
       "exists": { 
        "field": "locations" 
       } 
      } 
     }, 
    }, 
    "weight": 1000 
} 

因此缺失locations的記錄會因爲重量過大而首先出現。

語法會有所不同。在這裏查詢的詳細信息:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

+0

嗯,我會努力。我不明白一件事:爲什麼Elasticsearch跳過沒有'locations'字段的文檔?我把我的查詢放在'should'部分 - 而不是'must'。 – Bald