2016-09-29 77 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而不是所选的那个。任何想法如何调试/检查查询正在做什么? –