ElasticSearch - 整个文档的短语匹配?不只是一个特定领域
ElasticSearch - Phrase match on whole document? Not just one specific field
有没有办法在整个文档上使用弹性 match_phrase?不只是一个特定的领域。
我们希望用户能够输入带引号的搜索词,并在文档的任何位置进行短语匹配。
{
"size": 20,
"from": 0,
"query": {
"match_phrase": {
"my_column_name": "I want to search for this exact phrase"
}
}
}
目前,我只找到了特定字段的词组匹配。我必须指定要在其中进行短语匹配的字段。
我们的文档有数百个字段,所以我认为在每个 match_phrase 查询中手动输入 600 多个字段是不可行的。结果 JSON 会很大。
您可以使用带有类型短语的 multi-match query,在每个字段上运行 match_phrase 查询并使用来自最佳字段的 _score。请参阅短语和 phrase_prefix。
If no fields are provided, the multi_match query defaults to the
index.query.default_field index settings, which in turn defaults to *.
This extracts all fields in the mapping that are eligible to term queries and filters the metadata fields. All extracted fields are then
combined to build a query.
添加带有索引数据、搜索查询和搜索结果的工作示例
索引数据:
{
"name":"John",
"cost":55,
"title":"Will Smith"
}
{
"name":"Will Smith",
"cost":55,
"title":"book"
}
搜索查询:
{
"query": {
"multi_match": {
"query": "Will Smith",
"type": "phrase"
}
}
}
搜索结果:
"hits": [
{
"_index": "64519840",
"_type": "_doc",
"_id": "1",
"_score": 1.2199391,
"_source": {
"name": "Will Smith",
"cost": 55,
"title": "book"
}
},
{
"_index": "64519840",
"_type": "_doc",
"_id": "2",
"_score": 1.2199391,
"_source": {
"name": "John",
"cost": 55,
"title": "Will Smith"
}
}
]
您可以在匹配查询字段参数中使用 *,它将搜索文档中的所有可用字段。但它会降低你的查询速度,因为你正在搜索整个文档
有没有办法在整个文档上使用弹性 match_phrase?不只是一个特定的领域。
我们希望用户能够输入带引号的搜索词,并在文档的任何位置进行短语匹配。
{
"size": 20,
"from": 0,
"query": {
"match_phrase": {
"my_column_name": "I want to search for this exact phrase"
}
}
}
目前,我只找到了特定字段的词组匹配。我必须指定要在其中进行短语匹配的字段。
我们的文档有数百个字段,所以我认为在每个 match_phrase 查询中手动输入 600 多个字段是不可行的。结果 JSON 会很大。
您可以使用带有类型短语的 multi-match query,在每个字段上运行 match_phrase 查询并使用来自最佳字段的 _score。请参阅短语和 phrase_prefix。
If no fields are provided, the multi_match query defaults to the index.query.default_field index settings, which in turn defaults to *. This extracts all fields in the mapping that are eligible to term queries and filters the metadata fields. All extracted fields are then combined to build a query.
添加带有索引数据、搜索查询和搜索结果的工作示例
索引数据:
{
"name":"John",
"cost":55,
"title":"Will Smith"
}
{
"name":"Will Smith",
"cost":55,
"title":"book"
}
搜索查询:
{
"query": {
"multi_match": {
"query": "Will Smith",
"type": "phrase"
}
}
}
搜索结果:
"hits": [
{
"_index": "64519840",
"_type": "_doc",
"_id": "1",
"_score": 1.2199391,
"_source": {
"name": "Will Smith",
"cost": 55,
"title": "book"
}
},
{
"_index": "64519840",
"_type": "_doc",
"_id": "2",
"_score": 1.2199391,
"_source": {
"name": "John",
"cost": 55,
"title": "Will Smith"
}
}
]
您可以在匹配查询字段参数中使用 *,它将搜索文档中的所有可用字段。但它会降低你的查询速度,因为你正在搜索整个文档