我和elasticsearch:7.6.2一起跑
我有一个包含4个简单文档的索引:
PUT demo_idx/_doc/1
{
"content": "Distributed nature, simple REST APIs, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, the end"
}
PUT demo_idx/_doc/2
{
"content": "Distributed tmp nature, simple REST APIs, speed, and scalability"
}
PUT demo_idx/_doc/3
{
"content": "Distributed nature, simple REST APIs, speed, and scalability"
}
PUT demo_idx/_doc/4
{
"content": "Distributed tmp tmp nature"
}我想搜索文本:distributed nature并获得以下结果顺序:
Doc id: 3
Doc id: 1
Doc id: 2
Doc id: 4也就是说,与之完全匹配的文档(doc 3和doc 1)将在具有小斜率的文档(doc 2)之前显示,而具有大斜率匹配的文档将最后显示(doc 4)。
我读了这篇文章:如何构建一个考虑到单词之间的距离和单词的精确性的Elasticsearch查询,但对我没有帮助
我尝试了以下seach查询:
"query": {
"bool": {
"must":
[{
"match_phrase": {
"content": {
"query": query,
"slop": 2
}
}
}]
}
}但它并没有给我所需要的结果。
我得到了以下结果:
Doc id: 3 ,Score: 0.22949813
Doc id: 4 ,Score: 0.15556586
Doc id: 1 ,Score: 0.15401536
Doc id: 2 ,Score: 0.14397088如何编写查询以获得我想要的结果?
发布于 2022-03-29 04:34:31
您可以使用bool子句显示与“分布式性质”完全匹配的文档。第一个子句将提高那些与“分布式性质”完全匹配的文档的得分,而不需要任何斜率。
POST demo_idx/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"content": {
"query": "Distributed nature"
}
}
},
{
"match_phrase": {
"content": {
"query": "Distributed nature",
"slop": 2
}
}
}
]
}
}
}搜索响应将是:
"hits" : [
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.45899627,
"_source" : {
"content" : "Distributed nature, simple REST APIs, speed, and scalability"
}
},
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.30803072,
"_source" : {
"content" : "Distributed nature, simple REST APIs, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, the end"
}
},
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "4",
"_score" : 0.15556586,
"_source" : {
"content" : "Distributed tmp tmp nature"
}
},
{
"_index" : "demo_idx",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.14397088,
"_source" : {
"content" : "Distributed tmp nature, simple REST APIs, speed, and scalability"
}
}
]更新1:
为了避免搜索查询评分中“字段长度”参数的影响,需要使用更新映射API禁用“内容”字段的“规范”参数
PUT demo_idx/_mapping
{
"properties": {
"content": {
"type": "text",
"norms": "false"
}
}
}之后,重新编制文档索引,这样规范就不会立即被删除。
现在点击搜索查询,搜索响应将按照您希望得到的顺序进行。
https://stackoverflow.com/questions/71652540
复制相似问题