Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Serialize Lucene Query or convert query to String and back to Query

Am using Lucene 4.7.2 version.

I have indexed some information and have create a UI to query on that interface. I create a BooleanQuery based on the user input,

example:

BooleanQuery bq = new BooleanQuery();
NumericRangeQuery<Integer> nrq = NumericRangeQuery.newIntRange("age", 20, 30, true, true);
bq.add(nrq, BooleanClause.Occur.MUST);

Term term = new Term("name", "einstein");
TermQuery termQuery = new TermQuery(term);
bq.add(termQuery, BooleanClause.Occur.MUST);

System.out.println(bq.toString());

This prints,

+age[20 TO 30] AND name:einstein

Lucene runs on a separate server and it expects Query object to perform search. As the Query or BooleanQuery is not serializable, am trying to convert above String query across and convert it to Query/Boolean object.

I found this idea of converting BooleanQuery.toString() to String and back to BooleanQuery but am not able to find any API to convert String query to Query type.

like image 440
Arun Chandrasekaran Avatar asked Nov 16 '25 19:11

Arun Chandrasekaran


1 Answers

use this:

String queryString="Name:alivaliolah";
Query QueryObj=new QueryParser("", perFieldAnalyzor).parse(queryString);
TopDocs topDocFounded = searcher.search(QueryObj, hitsPerPage);
like image 58
Ebrahim Amini Sharifi Avatar answered Nov 18 '25 10:11

Ebrahim Amini Sharifi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!