在 String 字段上指定关键字类型

Specifying keyword type on String field

我开始使用 hibernate-search-elasticsearch(5.8.2),因为它看起来很容易集成,无需编写任何代码即可保持 elasticsearch 索引最新。这是一个很酷的库,但我开始认为它只实现了一小部分 elasticsearch 功能。我正在使用无痛脚本过滤器执行查询,该过滤器需要访问一个字符串字段,索引映射中的类型为 'text',如果不启用字段数据,这是不可能的。但我不太热衷于启用它,因为它会消耗大量堆内存。以下是 elasticsearch 团队在我的案例中建议执行的操作:

Fielddata documentation

Before you enable fielddata, consider why you are using a text field for aggregations, sorting, or in a script. It usually doesn’t make sense to do so.

A text field is analyzed before indexing so that a value like New York can be found by searching for new or for york. A terms aggregation on this field will return a new bucket and a york bucket, when you probably want a single bucket called New York.

Instead, you should have a text field for full text searches, and an unanalyzed keyword field with doc_values enabled for aggregations, as follows:

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "my_field": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

不幸的是,我找不到使用休眠搜索注释的方法。有人可以告诉我这是否可行,或者我必须迁移到 vanilla elasticsearch 库而不使用任何包装器吗?

使用当前版本的 Hibernate Search,您需要为此创建一个不同的字段(例如,您不能对同一字段有不同的风格)。请注意,无论如何,这就是 Elasticsearch 在幕后所做的事情。

@Field(analyzer = "your-text-analyzer") // your default full text search field with the default name
@Field(name="myPropertyAggregation", index = Index.NO, normalizer = "keyword")
@SortableField(forField = "myPropertyAggregation")
private String myProperty;

它应该创建一个带有文档值的未分析字段。然后,您需要为聚合参考 myPropertyAggregation 字段。

请注意,我们将在未来的 Search 6 中的 API 中公开更多 Elasticsearch 功能。在 Search 5 中,APIs 是为 Lucene 设计的,我们不能破坏他们。