I have been trying to implement a hibernate search query that when "Foo B" is searched it would display records with say a 'title' of "Foo Bar", a 'description' of "Foo Bear", and a 'name' of "Foo Berry", but would *NOT* return a result set of records with say a 'title' of "Foo" and a 'name' of "Bar", or even a title of "Foo Aplha" and a 'name' of "Bar Stool", so basically searching for the phrase as one entity and not really breaking it up. My current hack solution finds the records containing "foo" and the ones containing "B" and cross referencing to get the result set, it works fairly well but I would rather do this all through hibernate for scale-ability reasons and I also need to be able to sort the result set on a certain field.
I found the following post https://forum.hibernate.org/viewtopic.php?f=9&t=1025502&hilit=phrase+wildcard+search with the described solution of http://stackoverflow.com/questions/15285117/how-to-search-fields-with-wildcard-and-spaces-in-hibernate-search though it doesn't seem to work, and
Code:
fullTextSession.getSearchFactory().getAnalyzer("searchtokenanalyzer");
doesn't exist. So I am quite confused on how to go about it.
Also for the sorting I have been trying essentially what is below and that doesn't throw errors, and it also doesn't work.
Code:
SortField field =new SortField(sortColumn, SortField.STRING);
Sort sort = new Sort(field);
Session hibernateSession = this.getSession();
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(booleanQuery, this.type).setSort(sort);
Any help is appreciated, thanks
EDIT: Not sure why it posted this twice, but as for the search query, I have been trying to use MultiPhraseQuery but have been having trouble figuring out how to do the enumeration. I am using Hibernate 4.1.1 / lucene 3.5, and found this example http://stackoverflow.com/questions/5075304/how-to-use-a-multiphrasequery
Code:
TermEnum te = reader.terms(new Term("field", "app"));
List<Term> termList = new LinkedList<Term>();
while(te.next()) {
Term t = te.term();
if (!t.field().equals("field") || !t.text().startsWith("app")) {
break;
}
termList.add(t);
}
Term[] terms = termList.toArray(new Term[0]);
I'm not sure how to use that 'reader' as 'IndexReader.terms(Term)' doesn't exist, someone on stackoverflow said to use the AtomicReader but that is not in lucene 3.5
What I've been playing with
Code:
@Override
public List<T> testingSearch() {
Session hibernateSession = this.getSession();
List<T> results;
FullTextSession fullTextSession = Search
.getFullTextSession(hibernateSession);
fullTextSession.beginTransaction();
Term firstTerm = new Term("jobTitle", "entry");
Term secondTerm = new Term("jobTitle", "art*");
Term[] tTerms = new Term[] { firstTerm, secondTerm };
MultiPhraseQuery multiPhrasequery = new MultiPhraseQuery();
try {
File index = new File("/var/lucene/indexes");
Directory indexDirectory = FSDirectory.open(index);
PrefixTermEnum reader = new PrefixTermEnum(IndexReader.open(indexDirectory), secondTerm);
TermEnum te = reader.terms(secondTerm);
List<Term> termList = new LinkedList<Term>();
while (te.next()) {
Term t = te.term();
if (!t.field().equals("jobTitle") || !t.text().startsWith(secondTerm.text())) {
break;
}
termList.add(t);
}
Term[] terms = termList.toArray(new Term[0]);
multiPhrasequery.add(firstTerm);
multiPhrasequery.add(secondTerm);
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(
multiPhrasequery, this.type);
results = hibQuery.list();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
results = null;
}
return results;
}
I pretty much have no idea what I'm doing, PrefixTermEnum does not have a method or property of 'terms' either, this is just what someone on stack recommended I do.