2015-10-28 53 views
3

simple_query_string我有一个指数建立了我的所有文档:弹性搜索数倍提升

{ 
    "mappings" { 
    "book" { 
     "_source": { "enabled": true }, 
     "properties": [ 
     "title": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" }, 
     "description": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" }, 
     "author": { "type": "string", "analyzer": "standard", "search_analyzer": "standard" } 
     ] 
    } 
    } 
} 

我通过推到这个所谓的“”的指标。

我想要做的是执行搜索与以下要求。假设用户输入类似“黄色大铲”

  1. 执行搜索用户输入的关键字的方式有三种:
    1. 由于是作为一个整体一句话:“简单的黄色铲”
    2. 作为集和关键字:“简单+黄+铲”
    3. 为一组或关键字:“简单|黄色|铲”
  2. 确保关键字组按优先顺序执行(BO osted):
    1. 全文第一
    2. AND'd第二
    3. 逻辑与的第三

使用简单的查询作品找到一个单一的搜索:

{ 
    "query": { 
    "simple_query_string": { 
     "query": "\"simple yellow shovel\"" 
    } 
    } 
} 

如何用boosting执行多重搜索? 或者我应该在索引字段上使用类似“匹配”的查询吗?

+0

为什么你有你的财产'not_analyzed'?这没有意义。始终分析您要运行文本搜索的字段。而且你没有优先考虑你的搜索领域。我认为匹配的标题应该比描述中的匹配具有更高的优先级? –

+0

我会修复'not_analyzed'部分。这是一个很大的“哎呀”。优先排序为了简洁起见我省略了。 –

回答

4

我不知道我是否得到了这一个正确的。我假设的 作者>标题优先顺序>描述

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "bool": { 
      "must": [ 
       { 
       "multi_match": { 
        "query": "simple yellow shovel", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "type": "phrase", 
        "boost": 10 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "bool": { 
      "must": [ 
       { 
       "multi_match": { 
        "query": "simple", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 5 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "yellow", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 5 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "shovel", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 5 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "simple", 
      "fields": [ 
       "author^7", 
       "title^3", 
       "description" 
      ], 
      "boost": 2 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "yellow", 
      "fields": [ 
       "author^7", 
       "title^3", 
       "description" 
      ], 
      "boost": 2 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "shovel", 
      "fields": [ 
       "author^7", 
       "title^3", 
       "description" 
      ], 
      "boost": 2 
      } 
     } 
     ] 
    } 
    } 
} 

会有人请确认这一点?您可以参考Boost Query链接了解更多信息。这是你想要的?

我希望这有助于!

编辑:用dis_max

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "dis_max": { 
      "tie_breaker": 0.7, 
      "queries": [ 
       { 
       "bool": { 
        "must": [ 
        { 
         "multi_match": { 
         "query": "simple yellow shovel", 
         "fields": [ 
          "author^7", 
          "title^3", 
          "description" 
         ], 
         "type": "phrase", 
         "boost": 10 
         } 
        } 
        ] 
       } 
       }, 
       { 
       "bool": { 
        "must": [ 
        { 
         "dis_max": { 
         "tie_breaker": 0.7, 
         "queries": [ 
          { 
          "multi_match": { 
           "query": "simple", 
           "fields": [ 
           "author^7", 
           "title^3", 
           "description" 
           ], 
           "boost": 5 
          } 
          }, 
          { 
          "multi_match": { 
           "query": "yellow", 
           "fields": [ 
           "author^7", 
           "title^3", 
           "description" 
           ], 
           "boost": 5 
          } 
          }, 
          { 
          "multi_match": { 
           "query": "shovel", 
           "fields": [ 
           "author^7", 
           "title^3", 
           "description" 
           ], 
           "boost": 5 
          } 
          } 
         ] 
         } 
        } 
        ] 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "simple", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 2 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "yellow", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 2 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "shovel", 
        "fields": [ 
        "author^7", 
        "title^3", 
        "description" 
        ], 
        "boost": 2 
       } 
       } 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

这似乎改写给我更好的结果ATLEAST在我的数据集。这是一个很好的理解来源dismax

请发挥很大的作用,看看你是否得到预期的结果。 使用Explain API的帮助。

+0

我认为这可以使用'dis_max'查询重写。应该更优雅,配置更好。 –

+0

即使'dis_max'更好,这是一个**优秀的*第一步。至少让我指出了正确的道路。如果有人用'dis_max'回应并且它能够工作,那将会赢得答案 - 否则这至少让我开始了! –

+0

感谢@EvaldasBuinauskas建议dis_max。我已经更新了我的答案。让我知道如果它不起作用 – ChintanShah25

2

我已经使用Dis Max Query重写了这个。请记住,你可以尝试不同的类型来获得更好的结果。看到这些:

  1. best_fields
  2. most_fields
  3. cross_fields

查询:

POST /your_index/your_type/_search 
{ 
    "query": { 
    "dis_max": { 
     "tie_breaker": 0.7, 
     "boost": 1.2, 
     "queries": [ 
     { 
      "multi_match": { 
      "query": "simple yellow showel", 
      "type": "phrase", 
      "boost": 3, 
      "fields": [ 
       "title^3", 
       "author^2", 
       "description" 
      ] 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "simple yellow showel", 
      "operator": "and", 
      "boost": 2, 
      "fields": [ 
       "title^3", 
       "author^2", 
       "description" 
      ] 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "simple yellow showel", 
      "fields": [ 
       "title^3", 
       "author^2", 
       "description" 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 

派息最多的查询会挑文件的得分是所有三个查询最多。我们给"type": "phrase""operator": "and"增加了额外的提升,同时我们保持最后的查询不变。