Adam James
Adam James

Reputation: 4191

How to use Hibernate Analyzer?

I am trying to search for special characters in my hibernate query, I use QueryParser.escape(String searchTerm) to put a '\' character in front of all the special characters to escape them properly.

However I have found out that the standard analyzer used to tokenize removes these special characters from the index, so even if you properly escape the term 'abc-def' if you try and search it you would have to search 'abc def'.

So what analyzer should I use/how should I specify for the analyzer not to remove the special character when indexing?

A snippet of my annotated class and query building below:

@Entity
@Table(name="jobReq")
@Indexed
public class JobReq {

@Id
@DocumentId
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@Field
@Column(name="jobId", nullable=false, unique=true)
private String jobId;

@Fields({@Field, @Field(name = "jobIdSort", analyze = Analyze.NO)})
@Column(name="jobIdSort", nullable=false, unique=true)
private String jobIdSort;

@Field
@Column(name="jobTitle", nullable=false)
private String jobTitle;

Query:

tempQ = (org.apache.lucene.search.Query) qb.keyword()
                                .wildcard()
                                .onField(allFields[i].getName().toString())
                                .matching(QueryParser.escape(termToFind) + "*")
                                .createQuery();
                    }   
                    bq.add(new BooleanClause(tempQ, BooleanClause.Occur.SHOULD));
                }
            }
        }
        //wrap Lucene query in an org.hibernate.Query
        hibQuery = fullTextSession.createFullTextQuery(bq, this.type).setSort(sort);
        results = hibQuery.list();
        System.out.println(bq);
        fullTextSession.getTransaction().commit();

Upvotes: 1

Views: 1674

Answers (1)

femtoRgon
femtoRgon

Reputation: 33351

In this case, I don't believe it makes any sense to use QueryParser.escape. That is designed to escape a string-form query in preparation for parsing. You aren't using a QueryParser, and have stated in comments you don't intend to. You mention that you tried keyword analyzer already. I suspect the problem there is that you were running it through the QueryParser.escape method, and it was adding an, in that context, extraneous backslash, preventing matches being found.


If you were inclined to use QueryParser instead, and utilize analysis, you might create something like this:

SearchFactory searchFactory = fullTextSession.getSearchFactory();
org.apache.lucene.queryParser.QueryParser parser = new QueryParser(defaultField, searchFactory.getAnalyzer(JobReq.class) );
/* 
 create your BooleanQuery, loop, whatever else
*/
    org.apache.lucene.search.Query query = parser.parse( allFields[i].getName().toString() + ":" + QueryParser.escape(termToFind) + "*" );
    bq.add(new BooleanClause(tempQ, BooleanClause.Occur.SHOULD));

hibQuery = fullTextSession.createFullTextQuery(bq).setSort(sort);
results = hibQuery.list();

Upvotes: 1

Related Questions