弹性搜索不区分大小写

时间:2016-04-12 12:10:12

标签: elasticsearch lucene spring-data

我有以下基于注释的弹性搜索配置,我已设置索引不被分析,因为我不希望这些字段被标记化:

    @Document(indexName = "abc", type = "efg")
    public class ResourceElasticSearch {
    @Id
    private String id;
    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String name;
    @Field(type = FieldType.String, store = true)
    private List<String> tags = new ArrayList<>();
    @Field(type = FieldType.String)
    private String clientId;
    @Field(type = FieldType.String, index = FieldIndex.not_analyzed)
    private String virtualPath;
    @Field(type = FieldType.Date)
    private Date lastModifiedTime;
    @Field(type = FieldType.Date)
    private Date lastQueryTime;
    @Field(type = FieldType.String)
    private String modificationId;
    @Field(type = FieldType.String)
    private String realPath;
    @Field(type = FieldType.String)
    private String extension;
    @Field(type = FieldType.String)
    private ResourceType type;

是否可以通过使用注释使名称,virtualPath和标签上的搜索不区分大小写? 搜索看起来像这样,需要通过通配符搜索:

private QueryBuilder getQueryBuilderForSearch(SearchCriteria criteria) {
    String virtualPath = criteria.getPath();

    return boolQuery()
            .must(wildcardQuery("virtualPath", virtualPath))
            .must(wildcardQuery("name", criteria.getName()));
}

3 个答案:

答案 0 :(得分:5)

不太可能你想做什么,它不是关于Spring Data配置,而是关于Elasticsearch本身:你将数据索引为not_analyzed并且它将保持这种状态。

此外,如果您想要不区分大小写的数据,我建议使用keyword analyzerlowercase token filter进行索引。

答案 1 :(得分:1)

我根据Andrei Stefan的建议发现了一些与使用注释类似的结果:

    @Bean
    public Client client() throws IOException {
    TransportClient client = new TransportClient();
    TransportAddress address = new InetSocketTransportAddress(env.getProperty("elasticsearch.host"), Integer.parseInt(env.getProperty("elasticsearch.port")));
    client.addTransportAddress(address);

    XContentBuilder settingsBuilder = XContentFactory.jsonBuilder()
            .startObject()
            .startObject("analysis")
            .startObject("analyzer")
            .startObject("keyword")
            .field("tokenizer", "keyword")
            .array("filter", "lowercase")
            .endObject()
            .endObject()
            .endObject()
            .endObject();
    if (!client.admin().indices().prepareExists("abc").execute().actionGet().isExists()) {
        client.admin().indices().prepareCreate("abc").setSettings(settingsBuilder).get();
    }
       return client;
    }

答案 2 :(得分:0)

您可以添加消耗文件路径的@Setting,在@Document之后,设置文件应该包含这样的json: {"analysis":{"analyzer":{"case_insensitive":{"type":"custom","tokenizer":"whitespace","char_filter":["html_strip"],"filter":["lowercase","asciifolding"]}}}} 和带有分析器 @Field(type = FieldType.Keyword, analyzer = "case_insensitive")

的字段注释