Spring Elasticsearch HashMap [String,String]映射值不能not_analyzed

Son*_*son 3 java spring hashmap elasticsearch spring-data-elasticsearch

其实我的问题很简单:我希望我的hashmap值not_analyzed!

现在我有一个对象包含一个hashmap [string,string],看起来像:

class SomeObject{
    String id;
    @Field(type=FieldType.Object, index=FieldIndex.not_analyzed)
    Map<String, String> parameters;
}
Run Code Online (Sandbox Code Playgroud)

然后Spring数据elasticsearch在开始时生成这样的映射:

{
    "id": {
      "type": "string"
    },
    "parameters": {
      "type": "object"
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在我向es添加一些对象之后,它会添加更多这样的属性:

{
    "id": {
      "type": "string"
    },
    "parameters": {
        "properties": {
             "shiduan": {
                "type": "string"
             },
             "??": {
                "type": "string"
             }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,由于参数的值被分析,所以不能用es搜索,我的意思是不能搜索中文值,我试过我此时可以搜索英文.

然后,在阅读本帖后/sf/answers/2243105931/,我手动更新了映射:

{
    "id": {
      "type": "string"
    },
    "parameters": {
        "properties": {
             "shiduan": {
                "type": "string",
                "index": "not_analyzed"
             },
             "??": {
                "type": "string",
                "index": "not_analyzed"
             }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我现在可以搜索中文,所以我知道这个问题"没有被分析",就像所说的那样.

最后,任何人都可以告诉我如何使地图值"not_analyzed",我有google和stackoverflow很多次仍然找不到答案,让我知道是否有人可以帮助,非常感谢.

Val*_*Val 6

实现此目的的一种方法是mappings.json在构建路径上创建一个文件(例如yourproject/src/main/resources/mappings),然后使用@Mapping类中的注释引用该映射.

@Document(indexName = "your_index", type = "your_type")
@Mapping(mappingPath = "/mappings/mappings.json")
public class SomeObject{
    String id;
    @Field(type=FieldType.Object, index=FieldIndex.not_analyzed)
    Map<String, String> parameters;
}
Run Code Online (Sandbox Code Playgroud)

在该映射文件中,我们将添加一个动态模板,该模板将以parametershashmap 的子字段为目标,并将它们声明为not_analyzed字符串.

{
  "mappings": {
    "your_type": {
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "string",
            "path_match": "parameters.*",
            "mapping": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您需要确保先删除your_index然后重新启动应用程序,以便可以使用正确的映射重新创建它.