弹性搜索多个带有boost的simple_query_string

el *_*00b 4 elasticsearch

我为所有文档设置了索引:

{
  "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" }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我将其推入名为“ library ” 的索引中。

想要做的就是执行下列要求进行搜索。假设用户输入了类似“大黄铲”的内容

  1. 通过以下三种方式执行对用户输入的关键字的搜索:
    1. 就像整个短语一样:“简单的黄色铲子”
    2. 作为一组AND关键字:“简单+黄色+铲子”
    3. 作为一组OR关键字:“ simple | yellow | shovel”
  2. 确保按优先级顺序(增强吗?)执行关键字集:
    1. 全文优先
    2. 与第二
    3. 或第三

使用简单的查询可以查找单个搜索:

{
  "query": {
    "simple_query_string": {
      "query": "\"simple yellow shovel\""
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如何通过增强执行多重搜索?还是应该在索引字段上使用类似“匹配”的查询?

Chi*_*h25 5

我不确定我是否正确。我假设作者>标题>描述的优先顺序

{
  "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
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有人可以验证一下吗?您可以参考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
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这似乎给我的数据集至少带来了更好的结果。这是了解maxmax的重要资料

请对此进行很多尝试,看看您是否获得了预期的结果。使用Explain API的帮助。