【问题标题】:Filter not working for weighted search过滤器不适用于加权搜索
【发布时间】:2018-05-02 15:10:00
【问题描述】:

我对弹性搜索很陌生,并没有真正掌握它。所以我有一个搜索,其结果将根据其标签的权重进行加权,这绝对可以正常工作,但后来当我引入过滤器时,搜索总是给我空结果。这是我尝试过的:

{
          "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,
        }
      }

谢谢!

【问题讨论】:

  • 您能出示一份您希望匹配的文件吗?
  • 当然,我已经更新了问题。

标签: elasticsearch kibana


【解决方案1】:

您不能在tags 的嵌套查询中匹配type.primary。试试这个查询:

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

【讨论】:

  • 哦,我的错,我没有看到您的 function_score 在 nested 查询中。您不能在 tags 的嵌套查询中匹配 type.primary。让我更新我的答案
  • 酷,很高兴它有帮助!
  • 你能帮我解决一下stackoverflow.com/questions/50132182/… aswel
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 2019-11-11
  • 2020-12-26
  • 1970-01-01
相关资源
最近更新 更多