Teddy Kossoko
Teddy Kossoko

Reputation: 1322

Spring: Case insensitive search in Elasticsearch

I use spring 2.2.4.RELEASE and spring-data-elasticsearch 3.2.4.RELEASE

My elastic version is 7.10. I already have lot of index and data in it.

I just notice after once year that the search is not searching case insensitive.

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);

my index looks like this

    "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.

Upvotes: 0

Views: 38

Answers (1)

Vadim Yemelyanov
Vadim Yemelyanov

Reputation: 442

Using a match query, we may only search by the full title, which is also case-sensitive.


I suggest you need something like search client text in multiple fields?

Then you have 2 options

  1. Add copy_to parameter to your fields and then search on 1 field. This is very efficient and effective in terms of performance https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html
  2. If you don't want to reindex your documents - you can build should + wildcard queries example below ( Its in Kotlin, but I think you will get how to do this)
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)
            )
        }
       }
       

Upvotes: 0

Related Questions