Using solr 5.2.0 was wondering is there a query builder API/Jar/Ckient similar to Elasticsearch query builder API or do we have to bassically do String Kungfu to build queries in Solr?
Is there a SolrJ query builder?
Is solrtutorial.com/lucene-query-builder.html what you're looking for ? –
Angelynanger
Any API/SDK/jar –
Fukien
I guess we can use the lucene query builder classes directly. –
Fukien
Unfortunately, in SolrJ there is no such thing as a Builder for the query that goes into the q
-Parameter.
BUT: As Solr already operates on Lucene, we can as well use the Lucene QueryBuilder. The resulting Query objects (e.g. PhraseQuery) have a toString()
method that provides you with the query string you would otherwise have to assemble by hand.
@seven.windisch can you post some examples –
Waite
In #25841994 someone asked the question the other way round (Querystring to query object). The question contains a nice example for our case here. –
Admission
Be aware, that a lucene Query instance works with Terms but on client side you have not FieldTypes/analyzer. Possible the flexible QueryParser is the better choise QueryNode. But I see no way to build queries with nested queries with this syntaxTree neither. –
Psychosis
There is a boolean query builder that is designed to generate a string to be used with SolrJ's SolrQuery.
Here is a query generated by the code:
+(
(
title:"Grand Illusion"~1
title:"Paradise Theatre"~1
)^0.3
(
title:"Night At The Opera"~1
title:"News Of The World"~1
)^0.3
(
title:"Van Halen"~1
title:1984~1
)^0.3
)
And here is the code:
TermGroup group = new TermGroup().with(Occur. MUST);
TermGroup favoriteStyx = group.addGroup().withBoost(0.3f);
TermGroup favoriteQueen = group.addGroup().withBoost(0.3f);
TermGroup favoriteVanHalen = group.addGroup().withBoost(0.3f);
favoriteStyx.addTerm(new Term("title","Grand Illusion").with(Occur.SHOULD).withProximity(1));
favoriteStyx.addTerm(new Term("title","Paradise Theatre").with(Occur.SHOULD).withProximity(1));
favoriteQueen.addTerm(new Term("title","Night At The Opera").with(Occur.SHOULD).withProximity(1));
favoriteQueen.addTerm(new Term("title","News Of The World").with(Occur.SHOULD).withProximity(1));
favoriteVanHalen.addTerm(new Term("title","Van Halen").with(Occur.SHOULD).withProximity(1));
favoriteVanHalen.addTerm(new Term("title","1984").with(Occur.SHOULD).withProximity(1));
You might want to use SolrQuery
SolrQuery solrQuery=new SolrQuery();
solrQuery.set("q",query);
solrQuery.set("rows",5000);
QueryResponse response=solrServers.query(solrQuery);
For more examples please refer this link
I know. But the query still has to be build using string concatenation... I'm looking for a builder style API. It seems spring has something, but don't want to use spring. So far it seems it doesn't exist. –
Fukien
© 2022 - 2024 — McMap. All rights reserved.