一个字段的加权搜索和其他字段的普通搜索

时间:2018-05-02 10:16:43

标签: elasticsearch kibana

我正在尝试通过将搜索查询与标记或文档名称相匹配来执行搜索,我在顶部也有一个过滤器,因此我必须使用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的过滤器为我提供所有结果。

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

1 个答案:

答案 0 :(得分:1)

你快到了!在您的必须中,您声明标记和名称都必须命中。请尝试以下方法:

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"
                }
              }
            ]
          }
        }
      ]
    }
  }
}
相关问题