Elasticsearch - field_value_factor中的嵌套值查询

时间:2016-09-29 15:17:20

标签: elasticsearch nested

我是ES的新手,我正在为用户创建一个可搜索的产品目录,但我无法解决对购买相同产品的不同用户进行编码的方法。

我有一个完整的产品索引,这些产品可能已被我用嵌套表示的不同用户多次购买。有些产品有所有用户的条目,有些产品没有。

我需要创建搜索产品的能力,并且让特定用户购买的产品得分高于其他产品。我的问题是我不知道如何在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并且用户特定的评分提升不起作用。当nested``path围绕整个function_score应用multi_match上的第一个description查询并且name不起作用时。{/ p>

编辑1

另一种方法可能是分别计算得分,然后将它们组合起来。我可以这样做,但是should组合它们的方法会将得分归一化,这不是我想要的。所以我没有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"
}

2 个答案:

答案 0 :(得分: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
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

答案 1 :(得分:0)

首先需要将嵌套用户的条件构建到<!doctype html> <!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]--> <!--[if (IE 7)&!(IEMobile)]><html class="no-js lt-ie9 lt-ie8" lang="en"><![endif]--> <!--[if (IE 8)&!(IEMobile)]><html class="no-js lt-ie9" lang="en"><![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="en"><!--<![endif]--> <head> {% include _head.html %} </head> <body class="page"> {% include _browser-upgrade.html %} {% include _navigation.html %} {% if page.image.feature %} <div class="image-wrap"> <img src= {% if page.image.feature contains 'http' %} "{{ page.image.feature }}" {% else %} "{{ site.url }}/images/{{ page.image.feature }}" {% endif %} alt="{{ page.title }} feature image"> {% if page.image.credit %} <span class="image-credit">Photo Credit: <a href="{{ page.image.creditlink }}">{{ page.image.credit }}</a></span> {% endif %} </div><!-- /.image-wrap --> {% endif %} <div id="main" role="main"> <div class="article-author-side"> {% include _author-bio.html %} </div> <article class="page"> <h1>{{ page.title }}</h1> <div class="article-wrap"> {{ content }} {% if page.share != false %} <hr /> {% include _social-share.html %} {% endif %} </div><!-- /.article-wrap --> {% if site.owner.disqus-shortname and page.comments == true %} <section id="disqus_thread"></section><!-- /#disqus_thread --> {% endif %} </article> </div><!-- /#index --> <div class="footer-wrap"> <footer> {% include _footer.html %} </footer> </div><!-- /.footer-wrap --> {% include _scripts.html %} </body> </html> 查询中,然后将nested查询包装起来:

function_score