Tomas Bisciak
Tomas Bisciak

Reputation: 2841

Hibernate Search java spring, searching only entities that have ids as specified

I have a problem when i want to search in entities that have specific ids. I have fullTextQuery that i execute, it works all fine, bud when i want to say

ONLY SEARCH IN THESE ENTITIES (List of ids provided) :

+(title:slovakia~2 leadText:slovakia~2 body:slovakia~2 software:slovakia~2) +verified:true +eid:(113 | 112 | 3)

enter image description here

Then i get 0 results, these entities are indexed and persisted, all should be working fine, yet it doesnt return any results.

Here is The entity property defined :

@Id
@GeneratedValue
@Field(name = "eid")
@FieldBridge(impl = LongBridge.class)
private long id;

I have tried, Without field bridge, with TermVector.YES and also without any additional @Field.. annotation. All results either exception or just no results.

What is a proper way of searching in specific IDs?

For instance here is the working query: enter image description here

Creation of query looks like this :

    return Optional.of(getQueryBuilder()
            .keyword()
            .onField("eid")
            .matching(stringBuilder.toString())
            .createQuery());

Upvotes: 1

Views: 721

Answers (2)

Thomas Mwania
Thomas Mwania

Reputation: 405

From the little experience i have had with hibernate-search, only Ranges seem to work well with intenger and long fields. In your example here, i expect the following query should work just fine:

QueryBuilder qb = getQueryBuilder();
BooleanJunction<?> idJunction = qb.bool();
 bool.must(NumericRangeQuery.newLongRange("eid", Long.valueOf(eid), Long.valueOf(eid), true, true).createQuery();

In this case, the Boxed Long.valueOf() is optional if the values being supplied are Long values already.

Upvotes: 0

yrodiere
yrodiere

Reputation: 9977

The syntax you tried to use, (113 | 112 | 3), is not correct in this context. Parameters to the keyword query are not interpreted, in particular operators are not supported.

Use a boolean junction that matches any of the provided IDs instead:

List<String> eids = ...;

QueryBuilder qb = getQueryBuilder();

BooleanJunction<?> idJunction = qb.bool();

for (String eid : eids) {
    idJunction.should(
            qb.keyword()
                .onField("eid")
                .matching(eid)
                .createQuery()
    );
}

return idJunction.createQuery();

Note that, if you want to add other queries, you should not use the same junction. Use another junction that includes idJunction.createQuery() as one of its clauses.

Upvotes: 2

Related Questions