Question

I am working on facet search, one of the requirement is to select id values from facet drop-down. id values will be in the range 1-digit to 16-digit, but in data these id values will be from 8-digit to 16-digit.

I have to support following use-cases, both from UI and REST api.

Sample Query:

http://localhost:7001/app/api/search/meta.type:typeA AND id:value?results=facet:id

Requirement is to correct query, based on values passed for attribute id. If passed value's length is less than 8 digits. i.e. if the is less than 99999999, I have to do a regex search, otherwise normal matching search.
Ex:

1)
actual: +meta.type:typeA +id
modified +meta.type:typeA +id:8 +id:8*
2)
actual +meta.type:typeA +id:4567
modified +meta.type:typeA +id:4567 +id:4567*
3)
actual +meta.type:typeA +id:12345678
modified +meta.type:typeA +id:12345678 +id:12345678*
4)
actual +meta.type:typeA +id:123456789
modified +meta.type:typeA +id:123456789

Query type is BooleanQuery.

I have tried following, but it has no effect on query value. (Adding another value to the query as the first one wouldn't return anything)

Query [] termArray = new Query [1];
BooleanQuery bq = new BooleanQuery();
bq.add(new TermQuery(new Term("id", queryKeyValue + "*")), BooleanClause.Occur.MUST);
termArray[0] = Query.mergeBooleanQueries(bq);;
query.combine(termArray);

I am using Lucene 3.6 libraries.
I have a doubt, this kind of updates on query are possible or not.
If this is valid scenario and can be achieved, can anyone help me with this?

Was it helpful?

Solution

Don't use a TermQuery. TermQuery is a simple query exactly matching the given text. If you are manually constructing a query, there isn't a query parser to handle that sort of thing for you, so you have to select the correct types of queries yourself.

PrefixQuery is the one you are looking for:

bq.add(new PrefixQuery(new Term("id", queryKeyValue)), BooleanClause.Occur.MUST);

I don't think this will do what you want, however. The difference between

+meta.type:typeA +id:12345678

and

+meta.type:typeA +id:12345678 +id:12345678*

Is actually quite trivial. You will not get more results that way, because you still require an exact id match. I believe what you are looking for is probably more like:

+meta.type:typeA +(id:12345678 id:12345678*)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top