ElasticSearch 索引与搜索时间分析器

标签 elasticsearch analyzer synonym

遇到一个问题,这让我觉得我没有完全理解 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/

相关文章:

elasticsearch - Kibana如何知道(Elasticsearch)数据类型支持的聚合?

python - pip 安装不会向 pipFile 或 pipFile.lock 添加任何内容

lucene - 为什么 Lucene QueryParser 需要分析器

c# - Lucene .NET 的多短语同义词

java - 用于搜索的同义词数据结构

C#:使一个文件可从一个项目编辑,但将其包含在两个项目中

elasticsearch - random_score无法与 “should”一起正常使用

elasticsearch - Logstash JSON过滤器似乎不适用于JDBC输入

c# - Microsoft分析器为什么找不到Microsoft.CodeAnalysis?

elasticsearch - elasticsearch不分析领域