遇到一个问题,这让我觉得我没有完全理解 ElasticSearch 5.5 中的索引与搜索时间分析。
假设我有一个只有姓名
和状态
的人的基本索引。为了简单起见,我将 al => alabama
设置为唯一的州同义词。
PUT people
{
"mappings": {
"person": {
"properties": {
"name": {
"type": "text"
},
"state": {
"type": "text",
"analyzer": "us_state"
}
}
}
},
"settings": {
"analysis": {
"filter": {
"state_synonyms": {
"type": "synonym",
"synonyms": "al => alabama"
}
},
"analyzer": {
"us_state": {
"filter": [
"standard",
"lowercase",
"state_synonyms"
],
"type": "custom",
"tokenizer": "standard"
}
}
}
}
}
我的理解是,当我索引文档时,state
字段数据将作为扩展同义词形式进行索引。可以运行测试:
GET people/_analyze
{
"text": "al",
"field": "state"
}
返回
{
"tokens": [
{
"token": "alabama",
"start_offset": 0,
"end_offset": 2,
"type": "SYNONYM",
"position": 0
}
]
}
看起来不错,让我们索引一个文档:
POST people/person
{
"name": "dave",
"state": "al"
}
并执行搜索:
GET people/person/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"state": "al"
}
}
]
}
}
}
什么也不返回:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
我希望搜索中的 al
通过相同的 us_state
分析器运行并与我的文档匹配。但是,如果我将查询更改为:
"term": { "state": "阿拉巴马州"}
最佳答案
这是因为您使用了不分析输入的 term
查询。您应该将其更改为使用 match
查询,一切都会好起来的
GET people/person/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"state": "al"
}
}
]
}
}
}
关于ElasticSearch 索引与搜索时间分析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614230/