【问题标题】:Preserving the position while searching搜索时保留位置
【发布时间】:2014-05-21 07:19:37
【问题描述】:

我需要使用弹性搜索查询来处理多个单词。我正在使用edgeNgram tokenizer 来支持自动完成功能,并且我正在使用 query_string 进行搜索。

文档

{
   "title":"Gold digital cinema",
   "region":"Mumbai"
}
{
  "title":"cine park",
  "region":"Mumbai"
}
{
  "title":"Premier Gold",
  "region":"mumbai"
}

查询

{
  "query": {
  "bool": {
    "should": [
               {
                 "query_string": {
                     "fields": [
                                "title",
                                "region"
                           ],
                    "query":"gold cine"
                  }
               },
               {
                "fuzzy": {  
                  "title": {
                            "value":"gold cine",
                            "min_similarity": 0.5,
                            "max_expansions": 50,
                            "prefix_length": 0
                           }
                         }
               }
             ]
          }
       }
}

当我搜索黄金电影时,我需要 "title":"Gold digital cinema" 才能出现在顶部结果中。但我得到了"title":"cine park""title":"Premier Gold" 在顶部。

有什么方法可以在搜索时保留位置吗? 提前致谢。

更新

{
    "query":{
        "bool":{
            "should":[{
               "query_string":{
                "fields":["title.default_title^10",
                            "title.ngrams_front^2",
                             "title.ngrams_back"],
                                "query":"gold cine",
                                 "boost":2  
                               }
                      },
    { 
        "function_score":{
                   "query":{
                     "query_string":{
                        "fields":["region"],
                        "query":"MUMBAI"
                       }            
                   },
                     "functions":[{
                                    "script_score":{
                                        "script":"_score + 0.6"}    
                                }
                                ],
                                "score_mode":"max",
                                "boost_mode":"avg"
                           }
                      }
                ]
            }
        }
    }

【问题讨论】:

    标签: search elasticsearch


    【解决方案1】:

    您可以使用boost 将数据拉到顶部..

    {
    "query": {
    "bool": {
    "should": [
               {
                 "query_string": {
                     "fields": [
                                "title",
                                "region"
                           ],
                    "query":"gold cine",
                    "boost": 3
                  }
               },
               {
                "fuzzy": {  
                  "title": {
                            "value":"gold cine",
                            "min_similarity": 0.5,
                            "max_expansions": 50,
                            "prefix_length": 0
                           }
                         }
               }
             ]
          }
       }
    }
    

    如果您使用 1.0.0 及以上版本,则应使用function score query

    希望它有帮助..!

    【讨论】:

    • 感谢您的回答..我尝试按照您指定的方式进行提升。它给出与以前相同的结果。我尝试使用 function_score 查询,但它影响了其他文档的结果
    • @@BlackPOP,我使用 function_score 查询更新了代码。但我仍然没有达到预期的效果。如果您不介意,请告诉我如何实现它。?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多