[英]Not able to insert “index”:“not_analyzed” in mapping of elasticsearch 6.1.2
[英]Spring Elasticsearch HashMap[String, String] mapping value cannot be not_analyzed
其實我的問題很簡單:我希望我的hashmap值not_analyzed!
現在我有一個對象包含一個hashmap [string,string],看起來像:
class SomeObject{
String id;
@Field(type=FieldType.Object, index=FieldIndex.not_analyzed)
Map<String, String> parameters;
}
然后Spring數據Elasticsearch在開始時就生成了這樣的映射:
{
"id": {
"type": "string"
},
"parameters": {
"type": "object"
}
}
然后在我向es添加一些對象之后,它添加了更多這樣的屬性:
{
"id": {
"type": "string"
},
"parameters": {
"properties": {
"shiduan": {
"type": "string"
},
"季節": {
"type": "string"
}
}
}
}
現在,由於已分析參數的值,因此無法通過es進行搜索,我的意思是無法搜索中文值,我已經嘗試過此時可以搜索英語。
然后,在閱讀此帖子https://stackoverflow.com/a/32044370/4148034之后 ,我將映射手動更新為:
{
"id": {
"type": "string"
},
"parameters": {
"properties": {
"shiduan": {
"type": "string",
"index": "not_analyzed"
},
"季節": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
我現在可以搜索中文,所以我知道問題是“未分析的”,就像郵局所說的那樣。
最后,任何人都可以告訴我如何將地圖值設為“ not_analyzed”,我有很多次Google和stackoverflow仍然找不到答案,請讓我知道是否有人可以提供幫助,非常感謝。
實現此目的的一種方法是在構建路徑(例如yourproject/src/main/resources/mappings
)上創建一個mappings.json
文件,然后在類中使用@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;
}
在該映射文件中,我們將添加一個動態模板 ,該模板將定位parameters
hashmap的子字段並將其聲明為not_analyzed
字符串。
{
"mappings": {
"your_type": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"path_match": "parameters.*",
"mapping": {
"type": "string",
"index": "not_analyzed"
}
}
}
]
}
}
}
您需要確保先刪除your_index
,然后重新啟動您的應用程序,以便可以使用正確的映射重新創建它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.