首页 > 解决方案 > 是否可以在 spring-data-elasticsearch 中使用 Context Suggester 映射 Completion 字段?

问题描述

我想在像@CompletionField 这样的实体中映射字段,但是使用上下文,因为现在完成包括String []和int权重字段。我想过滤索引中的完成。

@Document(indexName = "compl_index")
    public class ComplIndex {
    @CompletionField
    private Completion suggestions;
   }

当我编写这个类时,我有一个简单的字符串数组和权重完成,但我想像这样映射实体,并使用上下文。我尝试解决这个问题 - 使用字段类型、上下文等编写一个新实体并使用映射进行注释,CompletionFieldMapper 抛出异常“字段不支持上下文字段:...

"name": {
          "type": "completion",
          "contexts": [
            {
              "name": "year",
              "type": "category",
              "path": "year"
            }
          ]
        },
        "year": {
          "type": "text"
        }

标签: javaspring-dataspring-data-elasticsearch

解决方案


它已经被支持,你可以在这里找到示例DATAES-536。对于较低版本,您需要编写自定义完成模型并使用字段@Mapping 而不是@CompletionField。

public class CustomCompletion {

    private String[] input;
    private Map<String, List<String>> contexts;
    private Integer weight;

    private CustomCompletion() {
        // required by mapper to instantiate object
    }

    public CustomCompletion(String[] input) {
        this.input = input;
    }

    // Setter getter

}

@Document(indexName = "compl_index")
public class ComplIndex {

    @Mapping(mappingPath = "/mapping/compl-index-suggestions.json")
    private CustomCompletion suggestions;

}

compl-index-suggestions.json

{
  "type": "completion",
  "contexts": [
    {
      "name": "year",
      "type": "category"
    }
  ]
}

推荐阅读