简体   繁体   中英

Hibernate-Search - Case insensitive wildcard search using lucene query parser syntax (not using QueryBuilder!)

Firstly, here is my Hibernate-Search indexing time setup:

// ...
@Indexed(index = "XXXRequestIndex")
@AnalyzerDef(name = "toLowercaseAnalyzer",
    tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class),
    filters = {
        @TokenFilterDef(factory = LowerCaseFilterFactory.class)
    })
public class XXXRequest implements Serializable {
    // ...
    @Field(analyze = Analyze.YES, store = Store.YES, analyzer = @Analyzer(definition = "toLowercaseAnalyzer"))
    @SortableField
    private String status;
    // ...
}

I saw this thread where .overridesForField(...) is setup on QueryBuilder for a field at query time in order to query case-insensitive with wildcards: Hibernate Search | ngram analyzer with minGramSize 1

I need to do something similar for a particular field only ("status"), but I am NOT using a QueryBuilder , instead I am parsing an incoming lucene query string using an MultiFieldQueryParser . I cannot change that to switch to build a query using a QueryBuilder because it is important for the callers of the code to issue their own dynamic queries using the lucene query parser syntax (like described more or less in https://lucene.apache.org/core/2_9_4/queryparsersyntax.html )

So when the caller sends as lucene query status:*n\\ Pr* , it does not match "In Processing". However a query like status:*n\\ pr* does match "In Processing".

My query code:

Analyzer analyzer = new KeywordAnalyzer();
String[] fields = ...
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
    fields,
    analyser);

Query luceneQuery = queryParser.parse(luceneFilterString);
List results = fullTextQuery.getResultList();

How can I make the query be case-insensitive?

Wildcard searches are not analyzed by the MultiFieldQueryParser so you have to do the filtering yourself (so you need to apply the filters manually to your input string).

Another option would be to use the simple query string feature (see https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#_simple_query_string_queries ), which can apply analysis to this sort of query but it only supports prefix queries (so you can search for process* but not *cessing ). If this limitation is acceptable for you, I really recommend this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM