如何搜索多个领域和总分?

laximzn5  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(1)|浏览(275)

我正试图找出一个解决方案,我应该如何组织我的查询,以找到人们问题的答案。例如,根据我将粘贴在本文末尾的数据集,我想查询“shows about romance”,可能会得到如下结果:

{
  "hits": [
    {
      "_score": "31",
      "_source": {
        "anime": "Grisaia no Kajitsu"
      }
    },
    {
      "_score": "12",
      "_source": {
        "anime": "Mirai Nikki"
      }
    },
    {
      "_score": "7",
      "_source": {
        "anime": "Bakemonogatari"
      }
    }
  ]
}

grisaia no kajitsu作为第一个结果出现,因为它在多个相关的问题中显示,mirai nikki是第二个,因为它比bakemonogatari得分更高。
基本上我希望答案是相关的基础上 question , score ,和 tags 现场。有重复答案的问题应该有更高的分数。有什么建议吗?
我的数据集:

[
  {
    "question": "Looking for romance anime",
    "score": 4,
    "answers": [
      {
        "anime": "Mirai Nikki",
        "score": 8,
        "tags": ["action", "adventure", "death game", "romance"]
      },
      {
        "anime": "Bakemonogatari",
        "score": 3,
        "tags": ["action", "comedy", "romance", "seinen"]
      }
    ]
  },
  {
    "question": "Survival Anime",
    "score": 10,
    "answers": [
      {
        "anime": "Grisaia no Kajitsu",
        "score": 4,
        "tags": ["school", "drama", "survival", "romance"]
      },
      {
        "anime": "Kanata no Astra",
        "score": 7,
        "tags": ["action", "comedy", "drama", "space"]
      }
    ]
  },
  {
    "question": "Horror and romance anime?",
    "score": 12,
    "answers": [
      {
        "anime": "Grisaia no Kajitsu",
        "score": 15,
        "tags": ["school", "drama", "survival", "romance"]
      }
    ]
  }
]
nnsrf1az

nnsrf1az1#

这应该适合你,你可以在这里调整各种增强参数,看看它如何影响你的结果

{
 "_source": ["answers.anime"],
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "answers.tags": {
                            "value": "Shows about romance",
                            "boost": 2 //weight of tags field
                        }
                    }
                },
                {
                    "match": {
                        "question": {
                            "query": "Shows about romance",
                            "boost": 2 //weight of question field
                        }
                    }
                },
                {
                    "function_score": {
                        "min_score": 0.9,
                        "functions": [
                            {
                                "field_value_factor": {
                                    "factor": 1, //weight of score field
                                    "field": "answers.score",
                                    "modifier": "log2p"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

相关问题