简体   繁体   中英

Fuzzy search on a phrase using Hibernate Search

I am using Hibernate Search to search for titles of tv shows on my web app.

I can use the method fuzzy() on keyword() in order to perfom fuzzy searches on keywords, but I need to take into account the whole title, so I am using phrase() instead of keyword() . The method fuzzy() is not defined for phrase() , so I was wondering if there is an easy way to achieve fuzzy searches on phrases using Hibernate Search.

If you just need a PhraseQuery with slop (that is, extra words thrown in), then you can set the slop on a phrase query like:

queryBuilder.phrase()
    .setSlop(2)
    .onField("myField")
    .sentance("this sentence missing something")
    .createQuery();

However, I'm not aware of anything in the Hibernate APIs that supports embedding fuzzy queries in phrases, but in Lucene, you can work with the SpanQuery API to build that. SpanMultiTermQueryWrapper and SpanNearQuery , in particular, are what you would need. Something like:

FuzzyQuery query1 = new FuzzyQuery(new Term("field", "fuzy"));
FuzzyQuery query2 = new FuzzyQuery(new Term("field", "phrse"));
Query wrappedQuery1 = new SpanMultiTermQueryWrapper<FuzzyQuery>(query1);
Query wrappedQuery2 = new SpanMultiTermQueryWrapper<FuzzyQuery>(query2);
SpanQuery[] clauses = {wrappedQuery1, wrappedQuery2};
SpanNearQuery(clauses, 0, true);

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