我对social mediatwo wordstag with many spaces这样的标签有疑问,搜索查询中每个单词的分数都增加了。

在搜索two wordstwo时如何实现将two words搜索为一个单词而不是获得不同的分数

这是当前结果得分的直观表示:

+-----------------------+-------+
| search                | score |
+-----------------------+-------+
| two                   | 2.76  |
| two words             | 5.53  |
| tag with many spaces  | 11.05 |
| singleword            | 2.76  |

这是我想要的图像:
+-----------------------+-------+
| search                | score |
+-----------------------+-------+
| two                   | 2.76  |
| two words             | 2.76  |
| tag with many spaces  | 2.76  |
| singleword            | 2.76  |

每个文档中都有多个标签。每个标签搜索都用PHP中的逗号,分解,并像下面的查询一样输出

假设文档具有多个标签,包括two wordssingleword,这将是搜索查询:
"query": {
    "function_score": {
        "query": {
            "bool": {
                "should": [
                    {
                        "match": {
                            "tags.name": "two words"
                        }
                    },
                    {
                        "match": {
                            "tags.name": "singleword"
                        }
                    }
                ]
            }
        },
        "functions": [
            {
                "field_value_factor": {
                    "field": "tags.votes"
                }
            }
        ],
        "boost_mode": "multiply"
    }
}

如果搜索two而不是two words,则得分会有所不同

这是搜索two words 时的结果
{
    "_index": "index",
    "_type": "type",
    "_id": "u10q42cCZsbFNf1W0Tdq",
    "_score": 4.708793,
    "_source": {
        "url": "example.com",
        "title": "title of the document",
        "description": "some description of the document",
        "popularity": 9,
        "tags": [
            {
                "name": "two words",
                "votes": 1
            },
            {
                "name": "singleword",
                "votes": 1
            },
            {
                "name": "othertag",
                "votes": 1
            },
            {
                "name": "random",
                "votes": 1
            }
        ]
    }
}

这是搜索two而不是two words 时的结果
{
    "_index": "index",
    "_type": "type",
    "_id": "u10q42cCZsbFNf1W0Tdq",
    "_score": 3.4481666,
    "_source": {
        "url": "example.com",
        "title": "title of the document",
        "description": "some description of the document",
        "popularity": 9,
        "tags": [
            {
                "name": "two words",
                "votes": 1
            },
            {
                "name": "singleword",
                "votes": 1
            },
            {
                "name": "othertag",
                "votes": 1
            },
            {
                "name": "random",
                "votes": 1
            }
        ]
    }
}

这是映射(专门用于标签)
"tags": {
  "type": "nested",
  "include_in_parent": true,
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "votes": {
      "type": "long"
    }
  }
}

我尝试用"\"two words\"""*two words*"搜索,但没有任何区别。

有可能实现这一目标吗?

最佳答案

您应使用未分析的字符串进行匹配,然后切换到术语查询。

你能试一下吗 :

"query": {
    "function_score": {
        "query": {
            "bool": {
                "should": [
                    {
                        "term": {
                            "tags.name.keyword": "two words"
                        }
                    },
                    {
                        "term": {
                            "tags.name.keyword": "singleword"
                        }
                    }
                ]
            }
        },
        "functions": [
            {
                "field_value_factor": {
                    "field": "tags.votes"
                }
            }
        ],
        "boost_mode": "multiply"
    }
}

在您的实际实现中,当您使用查询“两个单词”执行match查询时,它将分析您的查询以在标记中搜索标记“两个”和“单词”。因此,带有“两个单词”标签的文档将与两个标记匹配并得到增强。

关于elasticsearch - 多个单词在搜索中充当单个单词-Elasticsearch,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54058630/

10-13 08:25