2016-02-11 54 views
3

我想根據數組內嵌套對象中字段的權重修改ElasticSearch(v2 +)中的評分。基於嵌套字段值修改elasticsearch分數

例如,使用這樣的數據:

PUT index/test/0 
{ 
    "name": "red bell pepper", 
    "words": [ 
     {"text": "pepper", "weight": 20}, 
     {"text": "bell","weight": 10}, 
     {"text": "red","weight": 5} 
    ] 
} 

PUT index/test/1 
{ 
    "name": "hot red pepper", 
    "words": [ 
     {"text": "pepper", "weight": 15}, 
     {"text": "hot","weight": 11}, 
     {"text": "red","weight": 5} 
    ] 
} 

我要像{「words.text」:「紅辣椒」}查詢上面的「紅辣椒」,它會排「紅辣椒」。

我對這個問題的思考方式是「首先匹配'文本'字段,然後修改基於'權重'字段的評分。不幸的是,我不知道如何實現這一點,如果這甚至是可能的,或者如果我有正確的方法這樣的事情。

如果提出替代方法,請嘗試保留一個普遍的想法,其中有大量不同的類似情況(例如:簡單地將「紅椒」文檔分數修改爲更高並不是真正合適的替代方案)。

回答

4

您想到的方法是可行的。它可以通過function scorenested query中實現。

的示例性實現如下所示:

PUT test 

PUT test/test/_mapping 
{ 
    "properties": { 
     "name": { 
     "type": "string" 
     }, 
     "words": { 
     "type": "nested", 
     "properties": { 
      "text": { 
       "type": "string" 
      }, 
      "weight": { 
       "type": "long" 
      } 
     } 
     } 
    } 
} 


PUT test/test/0 
{ 
    "name": "red bell pepper", 
    "words": [ 
     {"text": "pepper", "weight": 20}, 
     {"text": "bell","weight": 10}, 
     {"text": "red","weight": 5} 
    ] 
} 
PUT test/test/1 
{ 
    "name": "hot red pepper", 
    "words": [ 
     {"text": "pepper", "weight": 15}, 
     {"text": "hot","weight": 11}, 
     {"text": "red","weight": 5} 
    ] 
} 

post test/_search 
{ 
    "query": { 
     "bool": { 
     "disable_coord": true, 
     "must": [ 
      { 
       "match": { 
        "name": "red pepper" 
       } 
      } 
     ], 
     "should": [ 
      { 
       "nested": { 
        "path": "words", 
        "query": { 
        "function_score": { 
         "functions": [ 
          { 
           "field_value_factor": { 
           "field" : "words.weight", 
           "missing": 0 
           } 
          } 
         ], 
         "query": { 
          "match": { 
           "words.text": "red pepper" 
          } 
         }, 
         "score_mode": "sum", 
         "boost_mode": "replace" 
        } 
        }, 
        "score_mode": "total" 
       } 
      } 
     ] 
     } 
    } 
} 

結果:

"hits": [ 
     { 
      "_index": "test", 
      "_type": "test", 
      "_id": "0", 
      "_score": 26.030865, 
      "_source": { 
       "name": "red bell pepper", 
       "words": [ 
        { 
        "text": "pepper", 
        "weight": 20 
        }, 
        { 
        "text": "bell", 
        "weight": 10 
        }, 
        { 
        "text": "red", 
        "weight": 5 
        } 
       ] 
      } 
     }, 
     { 
      "_index": "test", 
      "_type": "test", 
      "_id": "1", 
      "_score": 21.030865, 
      "_source": { 
       "name": "hot red pepper", 
       "words": [ 
        { 
        "text": "pepper", 
        "weight": 15 
        }, 
        { 
        "text": "hot", 
        "weight": 11 
        }, 
        { 
        "text": "red", 
        "weight": 5 
        } 
       ] 
      } 
     } 
     ] 
    } 

簡而言之的查詢將得分滿足must子句如下文檔:的所述weights總結匹配的嵌套文檔與must子句的得分。

+0

太棒了!正是我在找什麼。非常感謝!! – HotStuff68