Spring:Elasticsearch 中不区分大小写的搜索

问题描述 投票:0回答:1

我用弹簧

2.2.4.RELEASE
spring-data-elasticsearch 3.2.4.RELEASE

我的弹性版本是

7.10
。我已经有很多索引和数据了。

一年后我才注意到搜索不区分大小写。

MultiMatchQueryBuilder qb =  QueryBuilders.multiMatchQuery(criteria)
                .field("displayName")
                .field("authorName")
                .field("description")
                .field("shortDescription")
                .field("tag")
                .field("translatedMetadataList")
                .field("name")
                .field("packageName")
                .type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX);


        // we initialise the native search
        NativeSearchQueryBuilder nativeSearch  = new NativeSearchQueryBuilder()
                .withQuery(qb)
                .withPageable(PageRequest.of(page, size));

        // if the category is set, we return only this category
        if(categoryName.isPresent()) {
            nativeSearch .withQuery(QueryBuilders.matchQuery(CATEGORY_NAME, categoryName.get()));
        }

        // convert native to search
        SearchQuery searchQuery =  nativeSearch.build();
        Page<Game> gameList = gameRepo.search(searchQuery);

我的索引看起来像这样

    "mappings" : {
      "properties" : {
        "category" : {
          "type" : "nested",
          "include_in_parent" : true,
          "properties" : {
            "description" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "id" : {
              "type" : "long"
            },
            "name" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },...```



how could I do to search case insensitive ? 

Thank you.
spring-boot elasticsearch spring-data-elasticsearch
1个回答
0
投票

使用匹配查询,我们只能按完整标题进行搜索,这也是区分大小写


我建议您需要在多个字段中搜索客户端文本之类的东西?

那么你有2个选择

  1. copy_to 参数添加到您的字段,然后搜索 1 个字段。这在性能方面非常高效且有效 https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html

  2. 如果你不想重新索引你的文档 - 你可以构建下面的 should + 通配符查询示例(它在 Kotlin 中,但我想你会知道如何做到这一点)

    private fun buildSoftSearchByIdentifiers(searchByIdentifiers: Set<String>) = QueryBuilders.boolQuery().apply {
     searchByIdentifiers.forEach { identifier ->
         val likeQuery = "*${identifier.toLowerCase()}*"
         should(
             QueryBuilders.wildcardQuery(Profile::firstName.name, likeQuery)
         )
         should(
             QueryBuilders.wildcardQuery(Profile::lastName.name, likeQuery)
         )
     }
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.