ElasticSearch 匹配具有不同值的多个字段
ElasticSearch match multiple fields with different values
我实际上可以用这个进行简单的匹配搜索:
query: {match: {searchable: {query:search}}}
效果很好,我的可搜索字段在我的映射中进行了分析。
现在我想对多个字段进行搜索:1个字符串,其他都是数字。
我的映射:
mappings dynamic: 'false' do
indexes :searchable, analyzer: "custom_index_analyzer", search_analyzer: "custom_search_analyzer"
indexes :year, type: "integer"
indexes :country_id, type: "integer"
indexes :region_id, type: "integer"
indexes :appellation_id, type: "integer"
indexes :category_id, type: "integer"
end
def as_indexed_json(options={})
as_json(
only: [:searchable, :year, :country_id, :region_id, :appellation_id, :category_id]
)
end
我试过这个:
query: {
filtered: {
query: {
match: {
searchable: search
}
},
filter: {
term: {
country_id: "1"
},
term: {
region_id: "2"
},
term: {
appellation_id: "34"
}
}
}
},
sort: {
_score: {
order: :desc
},
year: {
order: :desc,
ignore_unmapped: true
}
},
size:100
它运行良好,但它在所有情况下都会给我 100 个结果,从 appellation_id 发送 (34),即使可搜索字段离文本搜索很远。
我也试过 BOOL 查询:
self.search(query: {
bool: {
must: [
{
match: {
country_id: "1"
},
match: {
region_id: "2"
},
match: {
appellation_id: "34"
},
match: {
searchable: search
}
}
]
}
},
sort: {
_score: {
order: :desc
},
year: {
order: :desc,
ignore_unmapped: true
}
},
size:100
)
但它会给我所有与可搜索字段匹配的结果,而不会处理 appellation_id 想要的。
我的目标是得到最好的结果和性能,要求ES给我所有country_id=X, region_id=Y, appellation_id=Z的数据,最后执行这组结果与可搜索字段的匹配...并且不要使用可搜索文本获得与现实相去甚远的结果。
谢谢。
正如您所知,elasticsearch match
基于 relevance score 查询 returns 结果。您可以尝试使用 term
查询而不是匹配来进行精确的术语匹配。另外我猜你的 bool 查询结构一定是这样的:
bool: {
must: [
{ match: {
country_id: "1"
}},
{match: {
region_id: "2"
}},
{match: {
appellation_id: "34"
}},
{match: {
searchable: search
}}
]
}
我实际上可以用这个进行简单的匹配搜索:
query: {match: {searchable: {query:search}}}
效果很好,我的可搜索字段在我的映射中进行了分析。
现在我想对多个字段进行搜索:1个字符串,其他都是数字。
我的映射:
mappings dynamic: 'false' do
indexes :searchable, analyzer: "custom_index_analyzer", search_analyzer: "custom_search_analyzer"
indexes :year, type: "integer"
indexes :country_id, type: "integer"
indexes :region_id, type: "integer"
indexes :appellation_id, type: "integer"
indexes :category_id, type: "integer"
end
def as_indexed_json(options={})
as_json(
only: [:searchable, :year, :country_id, :region_id, :appellation_id, :category_id]
)
end
我试过这个:
query: {
filtered: {
query: {
match: {
searchable: search
}
},
filter: {
term: {
country_id: "1"
},
term: {
region_id: "2"
},
term: {
appellation_id: "34"
}
}
}
},
sort: {
_score: {
order: :desc
},
year: {
order: :desc,
ignore_unmapped: true
}
},
size:100
它运行良好,但它在所有情况下都会给我 100 个结果,从 appellation_id 发送 (34),即使可搜索字段离文本搜索很远。
我也试过 BOOL 查询:
self.search(query: {
bool: {
must: [
{
match: {
country_id: "1"
},
match: {
region_id: "2"
},
match: {
appellation_id: "34"
},
match: {
searchable: search
}
}
]
}
},
sort: {
_score: {
order: :desc
},
year: {
order: :desc,
ignore_unmapped: true
}
},
size:100
)
但它会给我所有与可搜索字段匹配的结果,而不会处理 appellation_id 想要的。
我的目标是得到最好的结果和性能,要求ES给我所有country_id=X, region_id=Y, appellation_id=Z的数据,最后执行这组结果与可搜索字段的匹配...并且不要使用可搜索文本获得与现实相去甚远的结果。
谢谢。
正如您所知,elasticsearch match
基于 relevance score 查询 returns 结果。您可以尝试使用 term
查询而不是匹配来进行精确的术语匹配。另外我猜你的 bool 查询结构一定是这样的:
bool: {
must: [
{ match: {
country_id: "1"
}},
{match: {
region_id: "2"
}},
{match: {
appellation_id: "34"
}},
{match: {
searchable: search
}}
]
}