在一个字段上进行加权搜索,在另一字段上进行常规搜索 [英] Weighted search on one field and a normal search on other field

查看:76
本文介绍了在一个字段上进行加权搜索,在另一字段上进行常规搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将搜索查询与标签或文档名称匹配来执行搜索,在顶部也有一个过滤器,因此我必须使用must.

I am trying to perform a search by matching the search query to either the tag or the name of the doc, I also have a filter on the top, so I do have to use must.

这就是我一直在尝试的东西,

Here is what I have been trying,

{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "type.primary": "audio"
        }
      },
      "must": [
        {
          "nested": {
            "path": "tags",
            "score_mode": "sum",
            "query": {
              "function_score": {
                "query": {
                  "bool": {
                    "must": [
                      {
                        "match": {
                          "tags.tag": "big"
                        }
                      }
                    ]
                  }
                },
                "field_value_factor": {
                  "field": "tags.weight"
                },
                "boost_mode": "multiply",
                "boost": 10
              }
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "big",
                  "fields": [
                    "name"
                  ],
                  "type": "phrase_prefix"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

这只会导致空白.

如果我使用should而不是must,则查询工作正常,但是使用type.primary: audio过滤器,它为我提供了所有结果.

If I use should instead of must the query works fine, but it gives me all results with the filter of type.primary: audio.

我很确定还有其他搜索名称字段的方法.谢谢.

I am pretty sure there is some other way to search for the name field. Thanks.

推荐答案

您快到了!必须声明,必须同时击打标签和名称.请尝试以下操作:

You're almost there! In your must, you declare that both tags and name has to hit. Try the following:

GET /_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "type.primary": "audio"
        }
      },
      "must": [
        {
          "bool": {
            "should": [
              {
                "nested": {
                  "path": "tags",
                  "score_mode": "sum",
                  "query": {
                    "function_score": {
                      "query": {
                        "bool": {
                          "must": [
                            {
                              "match": {
                                "tags.tag": "big"
                              }
                            }
                          ]
                        }
                      },
                      "field_value_factor": {
                        "field": "tags.weight"
                      },
                      "boost_mode": "multiply",
                      "boost": 10
                    }
                  }
                }
              },
              {
                "multi_match": {
                  "query": "big",
                  "fields": [
                    "name"
                  ],
                  "type": "phrase_prefix"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

这篇关于在一个字段上进行加权搜索,在另一字段上进行常规搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆