一个字段加权搜索,另一个字段正常搜索

Weighted search on one field and a normal search on other field

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

这是我一直在尝试的,

{
  "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.

的过滤器为我提供所有结果

我很确定还有其他方法可以搜索名称字段。谢谢。

你就快完成了!在您的 must 中,您声明标签和名称都必须命中。请尝试以下操作:

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"
                }
              }
            ]
          }
        }
      ]
    }
  }
}