Is there a SolrJ query builder?
Asked Answered
F

3

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?

Fukien answered 22/2, 2017 at 13:8 Comment(3)
Is solrtutorial.com/lucene-query-builder.html what you're looking for ?Angelynanger
Any API/SDK/jarFukien
I guess we can use the lucene query builder classes directly.Fukien
A
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.

Admission answered 24/2, 2017 at 14:35 Comment(3)
@seven.windisch can you post some examplesWaite
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
H
0

There is a boolean query builder that is designed to generate a string to be used with SolrJ's SolrQuery.

Web Site

GitHub

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));
Hilleary answered 17/9 at 2:11 Comment(0)
S
-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

Sumac answered 22/2, 2017 at 18:40 Comment(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.Fukien

© 2022 - 2024 — McMap. All rights reserved.