ElasticSearch

mzsu5hc0  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(400)

我有一个标签问题,如 social media , two words , tag with many spaces 在搜索查询中,每个单词都有一个相乘的分数。
如何实现搜索 two words 作为一个词,而不是得到不同的分数时,搜索 two 以及 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 words 以及 singleword ,这将是搜索查询:

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

下面是Map(特别针对标记)

"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*" 但没什么区别。
有可能做到这一点吗?

kqhtkvqz

kqhtkvqz1#

您应该使用未分析的字符串进行匹配,并切换到术语查询。
你能试试吗:

"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 查询与查询“两个字”它将分析您的查询,以搜索标记中的标记“两个”和“字”。因此,标记为“two words”的文档将匹配这两个标记,并将得到提升。

相关问题