2016-09-29 76 views
0

我是ES新手,希望爲用戶創建一個可搜索的產品目錄,但我無法找到編碼購買相同產品的不同用戶的方式。Elasticsearch - field_value_factor裏面的嵌套值查詢

我有一個充滿產品的索引,而且這些產品可能已被我使用嵌套表示的不同用戶多次購買。一些產品有所有用戶的條目,有些則沒有。

我需要創建搜索產品的能力,並讓特定用戶購買的產品獲得更高的得分。我的問題是,我不知道如何在field_value_factor函數內部拉出這個字段,因爲它可能不適用於所有產品。

我最親密的嘗試,到目前爲止(感謝VAL)是:

{ 
    "query": { 
     "bool": { 
     "should": [ 
      { 
       "multi_match": { 
        "query": "black toner", 
        "fields": [ 
        "name", 
        "description" 
        ], 
        "tie_breaker": 0.3 
       } 
      }, 
      { 
       "query": { 
        "function_score": { 
        "query": { 
         "bool": { 
          "must": [ 
           { 
           "multi_match": { 
            "query": "black toner", 
            "fields": [ 
             "name", 
             "description" 
            ], 
            "tie_breaker": 0.3 
           } 
           }, 
           { 
           "nested": { 
            "path": "user", 
            "query": { 
             "term": { 
              "user.userid": "MWUser2" 
             } 
            } 
           } 
           } 
          ] 
         } 
        }, 
        "functions": [ 
         { 
          "field_value_factor": { 
           "field": "user.count", 
           "modifier": "log1p", 
           "missing": 0 
          } 
         } 
        ] 
        } 
       } 
      } 
     ] 
     } 
    } 
} 

這裏的問題是,我不能nested``path適用於field_value_factor,所以這總是出來爲0和用戶特定的得分提升不起作用。當在整個function_score周圍應用nested``path時,descriptionname上的第一個multi_match查詢不起作用。

編輯1

另一種方式來做到這一點可能是單獨計算得分,然後將它們結合起來。我可以做到這一點,但是將它們結合的方法會使分數正常化,這不是我想要的。所以,而不是做0.9 + 40.5 + 5我得到0.7+0.7兩者。有沒有辦法解決?

{ 
    "query": { 
     "bool": { 
     "should": [ 
      { 
       "query": { 
        "multi_match": { 
         "use_dis_max": false, 
        "query": "black super quality toner", 
        "fields": [ 
         "name^3", 
         "description" 
        ], 
        "tie_breaker": 0.3 
        } 
       } 
      }, 
      { 
       "query": { 
        "nested": { 
        "path": "user", 
        "query": { 
         "function_score": { 
          "filter": { 
           "term": { 
           "user.userid": "MWUser1" 
           } 
          }, 
          "functions": [ 
           { 
           "field_value_factor": { 
            "field": "user.count", 
            "modifier": "log1p", 
            "missing": 0 
           } 
           } 
          ] 
         } 
        } 
        } 
       } 
      } 
     ] 
     } 
    } 
} 

我的映射是:

{ 
    "mappings": { 
    "nest_type": { 
     "properties": { 
     "id" :    {"type":"string"}, 
     "company_code" : {"type":"string"}, 
     "name" :   {"type":"string"}, 
     "description" : {"type":"string"}, 
     "virtual_entity" : {"type":"boolean"}, 
     "created_at" :  {"type":"date"}, 
     "updated_at" :  {"type":"date"}, 
     "user": { 
      "type": "nested", 
      "properties": { 
      "userid": {"type":"string"}, 
      "count": {"type":"short"}, 
      "last_bought": {"type":"date"} 
      } 
     }, 
     "@timestamp" : {"type":"date"} 
     } 
    } 
    } 
} 

有些文件是:

{ 
    "id": "C8061X", 
    "company_code": "MWCOMPCODE", 
    "name": "Black LaserJet Toner Cartridge", 
    "description": "- HP LaserJet C8061 Family Print Cartridges deliver extra sharp black text, smooth greyscales and fine detail in graphics.\n- HP LaserJet C8061 Family Print Cartridges with Smart Printing Technology with in-built reliability and rigorous quality testing ensure maximum printer uptime with minimum user intervention.\n- HP LaserJet C8061 Family Print Cartridges all-in-one design allow effortless installation and maintenance. Smart Printing Technology features monitoring of supplies status and usage information via the printers control panel or web browser.\n", 
    "virtual_entity": false, 
    "created_at": "2016-09-21T12:23:53.000Z", 
    "updated_at": "2016-09-21T12:23:53.000Z", 
    "user": [ 
    { 
     "userid": "MWUser1", 
     "count": 4, 
     "last_bought": "2016-09-14T12:43:30.000Z" 
    }, 
    { 
     "userid": "MWUser2", 
     "count": 2, 
     "last_bought": "2016-09-14T10:00:00.000Z" 
    } 
    ], 
    "@timestamp": "2016-09-21T13:38:30.077Z" 
} 
{ 
    "id": "C8061Y", 
    "company_code": "MWCOMPCODE", 
    "name": "Black LaserJet Toner Cartridge Super Quality", 
    "description": "- HP LaserJet C8061 Family Print Cartridges deliver extra quality sharp black text, smooth greyscales and fine detail in graphics.\n- HP LaserJet C8061 Family Print Cartridges with Smart Printing Technology with in-built reliability and rigorous quality testing ensure maximum printer uptime with minimum user intervention.\n- HP LaserJet C8061 Family Print Cartridges all-in-one design allow effortless installation and maintenance. Smart Printing Technology features monitoring of supplies status and usage information via the printers control panel or web browser.\n", 
    "virtual_entity": false, 
    "created_at": "2016-09-21T12:23:53.000Z", 
    "updated_at": "2016-09-21T12:23:53.000Z", 
    "@timestamp": "2016-09-21T13:38:30.077Z" 
} 

回答

1

我最終做了以下。我確保文檔滿足全文搜索,並將得分建立爲全文評分和用戶計數記錄的提升組合。

GET /nest_index_toy/_search 
{ 
    "query": { 
     "bool": { 
     "must": { 
      "multi_match": { 
       "use_dis_max": false, 
       "query": "black toner super quality", 
       "fields": [ 
        "name^3", 
        "description" 
       ], 
       "tie_breaker": 0.3, 
       "boost": 2 
      } 
     }, 
     "should": [ 
      { 
       "multi_match": { 
        "use_dis_max": false, 
        "query": "black toner super quality", 
        "fields": [ 
        "name^3", 
        "description" 
        ], 
        "tie_breaker": 0.3, 
        "boost": 2 
       } 
      }, 
      { 
       "nested": { 
        "path": "user", 
        "query": { 
        "function_score": { 
         "filter": { 
          "term": { 
           "user.userid": "MWUser1" 
          } 
         }, 
         "functions": [ 
          { 
           "field_value_factor": { 
           "field": "user.count", 
           "modifier": "log1p", 
           "missing": 0 
           } 
          } 
         ] 
        } 
        } 
       } 
      } 
     ] 
     } 
    } 
} 
0

首先需要建立在嵌套的用戶狀態爲nested查詢,然後包裹你的function_score查詢:

{ 
    "query": { 
    "nested": { 
     "path": "user", 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "user.userid": "MWUser1" 
       } 
      }, 
      { 
       "function_score": { 
       "query": { 
        "multi_match": { 
        "query": "black toner", 
        "fields": [ 
         "name", 
         "description" 
        ], 
        "tie_breaker": 0.3 
        } 
       }, 
       "field_value_factor": { 
        "field": "user.userid.count", 
        "modifier": "log1p", 
        "missing": 10 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "size": 5 
} 
+0

感謝您的回覆,我不知道你可以把一個查詢放在function_score裏面,但是這並沒有給我想要的東西。 使用「必須」時不返回結果。當我使用「應該」時,僅返回具有「MWUser1」條目的文檔,但是他的「count」值未被使用。當查詢重新運行「MWUser2」時,「C8061X」文檔得到相同分數,但MWUser2.count是2而不是4. 如果此查詢只能根據選定的用戶計數返回分數,則I認爲我可以將它與另一個查詢結合起來進行全文匹配? –

+0

嗯,也許'name'和'description'上的'multi_match'查詢需要在頂層提取到'bool/must'中。你能試一下嗎? – Val

+0

啊 - 謝謝你。我**將'multi_match'複製到頂層,並使用'should'這個查詢返回沒有任何'用戶'信息的產品,以前購買的產品位於頂部。 但是,它看起來像是取第一個user.count而不是所選的那個。任何想法如何調試/檢查查詢正在做什麼? –