如何在 Spring 中将 ElasticSearch 模型字段标记为 KeyWord 类型?

How to mark ElasticSearch model field as KeyWord type in Spring?

我需要支持一个 Spring 应用程序使用 ElasticSearch 作为数据存储,我需要提取一些按术语过滤的数据,比如

POST http://localhost:1234/library/myType/_search
{
   "query": {
        "bool": {           
           "filter": {"term": {"myTextField": "filterValue"}}           
        }        
    }  
}

问题是 Java 模型中的字段被注释为

@Field(type = FieldType.String)

不像

@Field(type = FieldType.Keyword)

我已经尝试 google 关键字注释,但似乎有一个我无法透露的解决方法。如何注释模型字段以在查询中按术语过滤它?

keyword 数据类型是在 ES 5 中添加的,因此您不会在 spring-data-es 2.0.3 中找到它。

您需要将您的字段声明为 not_analyzed,即像这样:

@Field(type = FieldType.String, index = FieldIndex.not_analyzed)