8

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?

user432024
  • 4,392
  • 8
  • 49
  • 85

2 Answers2

9

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.

sven.windisch
  • 432
  • 2
  • 10
  • @seven.windisch can you post some examples – kozla13 Mar 28 '18 at 18:56
  • In https://stackoverflow.com/questions/25841494/how-to-serialize-lucene-query-or-convert-query-to-string-and-back-to-query someone asked the question the other way round (Querystring to query object). The question contains a nice example for our case here. – sven.windisch Mar 29 '18 at 10:38
  • 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](https://lucene.apache.org/core/7_5_0/queryparser/org/apache/lucene/queryparser/flexible/core/nodes/QueryNode.html). But I see no way to build queries with nested queries with this syntaxTree neither. – Karsten R. Dec 20 '18 at 14:24
-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

kpahwa
  • 723
  • 4
  • 7
  • 1
    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. – user432024 Feb 22 '17 at 20:36