1
votes

I have integrated the hibernate search 3.1.1 with my existing application with Spring 2.5 and Hibernate core 3.3.2 GA. With hibernate-search-3.1.1, I am using apache lucene 2.4.1.

The problem I am facing is when I search a single word or multiple words in order, it searches perfectly and return the result but when I search multiple words out of order with blank spaces, it does not return any result. For Example, If I have a text indexed as

"Hello great world!"

Now If I search "Hello" or "great world", it returns result successfully but if I search "world Hello", it returns no result.

What I want is to be able return result if any of the complete or partial words matches on the indexed text. My source code is as below:

FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(this.entityManager);

// create native Lucene query
String[] fields = new String[] { "text", "description", "standard.title", "standard.briefPurpose", "standard.name" };
MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
org.apache.lucene.search.Query query = null;

try {
    query = parser.parse(searchTerm);
} catch (ParseException e) {
    e.printStackTrace();
}

// wrap Lucene query in a javax.persistence.Query
FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Requirement.class);

// execute search
@SuppressWarnings("unchecked")
List<Requirement> result = persistenceQuery.getResultList();

return result;

Please help if I need to add any thing to support what I desire.

2

2 Answers

2
votes

I know its very old question. But for other this short description can be helpful.

You should use analyze = Analyze.YES attribute with @Field annotation in your pojo class. It divides the string value of that field into tokens i.e. into single words. So you can search in any sequence. But note that you have to enter whole world. For example to search 'United States of America' can be found with 'America', 'States', 'States United', 'America United', etc. But whole word will be required by Hibernate search. Only 'Uni' will not work.

EDIT:

For older version of Hibernate Search Apis:

One has to use Index.TOKENIZED instead of analyze = Analyze.YES with @Field annotation.

0
votes

Have you tried to use PhraseQuery.setSlop(int)? This should allow word reordering. Check out the Javadoc for more information.