我会建议尝试Elasticsearch的同义词特征。作为一个简单的原因,考虑到你的用户不会一直在使用“美国”,或者在他们的查询中一直使用“英国”。如果用户使用“美国”或“你的一个”或“州”或“英格兰”,该怎么办?对于这些情况,您可以使用此功能。
这里有一个出发点:
{
"settings": {
"analysis": {
"filter": {
"my_synonym_filter": {
"type": "synonym",
"synonyms": [
"u s a,united states,united states of america => usa",
"g b,gb,great britain,united kingdom, uk, u k => britain,england,scotland,wales",
"united arab emirates, emirates, arab emirates => emirates"
]
}
},
"analyzer": {
"my_synonyms": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_synonym_filter"
]
}
}
}
},
"mappings": {
"country": {
"properties": {
"name": {
"type": "string",
"analyzer": "my_synonyms"
}
}
}
}
}
而且,考虑到你在你的国家指数有这些国家:
{ "index": {}}
{ "name": "japan"}
{ "index": {}}
{ "name": "united kingdom"}
{ "index": {}}
{ "name": "united states"}
{ "index": {}}
{ "name": "united arab emirates"}
一种
{
"query": {
"match": {
"name": {
"query": "car dealerships in the uk, japan and emirates"
}
}
}
}
搜索会给你所有三个国家:
"hits": [
{
"_index": "my_index",
"_type": "country",
"_id": "CMZe2ygBS4OLL3_lT_B2_Q",
"_score": 0.03739948,
"_source": {
"name": "japan"
}
},
{
"_index": "my_index",
"_type": "country",
"_id": "T-e7rg_rTx-3rtTJYxJrBg",
"_score": 0.03739948,
"_source": {
"name": "united arab emirates"
}
},
{
"_index": "my_index",
"_type": "country",
"_id": "EqlMu2RiRiSdwyqJa2nyzA",
"_score": 0.017334092,
"_source": {
"name": "united kingdom"
}
}
]
而且如果查询只有一个国家,只有一个会被退回:
{
"query": {
"match": {
"name": {
"query": "car dealerships in the united states"
}
}
}
}
更多关于此功能here。