2016-07-07 120 views
2

其实我的问题很简单:我想我的hashmap值not_analyzed!Spring Elasticsearch HashMap [String,String]映射值不能被分析

我现在有一个对象包含一个HashMap [字符串,字符串],看起来像:

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

然后elasticsearch在beggining生成这样的映射弹簧数据:

{ 
    "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”,像帖子里说。

最后,任何人都可以告诉我如何使地图值“not_analyzed”,我有谷歌和stackoverflow许多次仍然找不到答案,让我知道如果有人可以帮助,非常感谢。

回答

5

实现此目的的一种方法是在构建路径上创建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; 
} 

在该映射文件中,我们要添加一个dynamic template将针对您的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,然后再重新启动应用程序,以便它可以与适当的映射重新创建。

+0

对不起,没有按时接受你的答案,它的工作原理。再次感谢你的帮助 –

相关问题