Hibernate Search: Searching by multiple equal words

I use Hibernate Search 5.10.3, and I cannot search results which contain only N equal words. For instance, having the following samples in my index:

1. foo foo
2. foo bar

I want to get only the line 1 matching "foo foo", so I make the following query :

queryBuilder.simpleQueryString().onField("myField").matching("foo + foo").createQuery();

But this query returns both "foo foo" and "foo bar". How to do this using hibernate search?

In general I need search all typed words in any order with considering of their count. 1)All words in search query must be in the field in any order 2) with the same count. I can reach 1) but have no idea how I can reach 2). Example:

1. foo bar smth
2. foo bar smth bar 

If I search "bar smth foo" I need only 1. if "bar foo bar smth" - only 2.

1 answer

  • answered 2018-11-08 14:00 yrodiere

    That's not the kind of thing that is easy to do with Lucene. You might want to reconsider your use case, see if full-text search really is the way to go.

    I don't have an exact solution to give you, but phrase queries look fairly similar to what you're trying to achieve.

    You could try something like this:

    queryBuilder.simpleQueryString().onField("myField").matching("foo smth \"bar bar\"").createQuery();
    

    The above will match two "bar" that strictly follow each other. If you want to be a bit more lenient, you can set the slop:

    queryBuilder.simpleQueryString().onField("myField").matching("foo smth \"bar bar\"~2").createQuery();
    

    This will match two "bar" that follow each other OR are separated by one or two words. The actual meaning is a bit more complicated, but you get the idea. You can find an exact definition of the slop in the javadoc of org.apache.lucene.search.PhraseQuery#getSlop.

    I suppose using a very high slop would achieve more or less what you want, but that might have a bad impact on performance.