Hibernate search exact match for strings with dashes

Viewed 1373

I'd like to do an exact search for entity field that has "xx-xx-xx" format.

The entity looks as follows:

@Entity
@Indexed
class Resource {

    @Field
    private String address; // has "xx-xx-xx" format
}

The query creation process is as follows: queryBuilder.keyword().onFields("resource").matching(searchQuery).createQuery()

Suppose I have two resources with the following addresses:

aa-bb-cc
cc-dd-ee

When I run a search query "aa-bb-cc" I expect that only the first resource will be returned, but instead, I have the search returns both resources.

What should I change to do and exact search by resource field?

1 Answers

You need to disable the default Analyzer for this field, so that it's treated as "exact" keywords:

@Entity
@Indexed
class Resource {

    @Field(analyze=Analyze.NO)
    private String address; // has "xx-xx-xx" format
}
Related