USQ91
USQ91

Reputation: 352

HIbernate Search not returning search results on primary key

I'm using HIbernate search(5.8.2). The search works smoothly on every field except for the primary key. It returns an empty list when I pass anything to it. I followed the hibernate documentation, used @documentId annotation for primary key. What am I missing?

Here's my model:

@SuppressWarnings("serial")
@Entity
@Indexed
@Table(name = "MAIN",schema="maindb")
public class MAIN implements Serializable {

@Id
@DocumentId
private String poNo; // my primary key which has values like "PO123"

@Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
private String postatus;

My search function:

public List<?> search(String poNumber, String status) {

QueryBuilder qb = 
fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(MAIN.class).get();

BooleanQuery.Builder finalLuceneQuery = new BooleanQuery.Builder();

org.hibernate.query.Query hibQuery = 
fullTextSession.createFullTextQuery(finalLuceneQuery.build(),MAIN.class);

org.apache.lucene.search.Query querypono1 = 
qb2.simpleQueryString().onField("poNo").matching(poNumber)
.createQuery();

org.apache.lucene.search.Query queryStatus =
qb.simpleQueryString().onField("po01_status")
.matching("postatus").createQuery();

finalLuceneQuery.add(querypono1, BooleanClause.Occur.MUST);
finalLuceneQuery.add(queryStatus , BooleanClause.Occur.MUST);

hibQuery.setFirstResult(0);
List<?> resultArchive = new ArrayList<String>();

    try {
        result = hibQuery.getResultList();
    } catch (Exception e) {
        e.printStackTrace();
        // log.log(ERROR, "ERROR FETCHING RESULT LIST FROM DATABASE");
    }

    return result;
}

Upvotes: 0

Views: 454

Answers (1)

Guillaume Smet
Guillaume Smet

Reputation: 10539

The issue is that "PO123" is transformed to "po123" by the simple query string parser. I wonder why, I have to check that, it's probably a bug, or it's at least an unexpected behavior.

That being said, you shouldn't use the simpleQuery() entry point for an exact matching.

Replace:

org.apache.lucene.search.Query querypono1 = qb2.simpleQueryString().onField("poNo").matching(poNumber).createQuery();

By:

org.apache.lucene.search.Query querypono1 = qb2.keyword().onField("poNo").matching(poNumber).createQuery();

(keyword() instead of simpleQueryString())

I will follow up on this issue though as it's not the behavior I would have expected. Thanks for raising it.

-> JIRA issue: https://hibernate.atlassian.net/browse/HSEARCH-3039 , will be included in the upcoming 5.10.0.Final.

Upvotes: 2

Related Questions