过滤不适用于加权搜索

时间:2018-05-02 06:44:54

标签: elasticsearch kibana

我对弹性搜索很新,并没有真正掌握它。所以我有一个搜索,其结果将根据他们的标签的权重进行权衡,这非常好,但后来当我介绍一个过滤器时,搜索总是给我空的结果。这是我尝试过的:

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

过滤器的示例结果应该是这样的:

{
        "_index": "assets",
        "_type": "Asset",
        "_id": "5a1dc3c0848662ee49e36f43s",
        "_score": 886.8744,
        "_source": {
          "name": "And Action Breakbeat",
          "meta_data": {
            "type": "audio/mp3",
            "file_name": "music_zapsplat_and_action_breakbeat.mp3"
          },
          "file_key": "music_zapsplat_and_action_breakbeat.mp3",
          "src": {
            "url": "https://exapmle.com/music_zapsplat_and_action_breakbeat.mp3"
          },
          "type": {
            "primary": "AUDIO",
            "secondary": "mp3"
          },
          "thumbnail_url": "https://example.com/thumbnail/audio.jpg",
          "tags": [
            {
              "tag": "big",
              "weight": 10
            },
            {
              "tag": "beat",
              "weight": 5
            },
            {
              "tag": "music",
              "weight": 3.3333333333333335
            }
          ],
          "isDeleted": false,
        }
      }

谢谢!

1 个答案:

答案 0 :(得分:1)

您无法在type.primary的嵌套查询中匹配tags。请尝试此查询:

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