2014-10-05 100 views
3

我在我的索引中有一个国家类型,其中包含国家名称列表。我想查找用户可能已将其放入查询的任何国家/地区名称。例如,如果用户搜索:Elasticsearch从查询字符串返回国家

car dealerships in japan 

然后我想返回国家日本。这适用于单个词的国家,如果我这样做:

GET /my_index/country/_search 
{ 
    "query": { 
     "match" : { 
      "name": { 
        "query": "car dealerships in japan" 
      } 
     } 

    } 
} 

返回国日本,这是我想做些什么。

但是,如果国家名称中有多个单词,我不确定如何才能识别此问题。否则,如果查询是这样的:

car dealerships in the united kingdom 

它会返回多个国家,如美国,英国,阿联酋......但我希望它只是返回英国此查询。

我不知道最好的方法来解决这个问题。

回答

1

我会建议尝试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

相关问题