I have added to SolrJ the ability to use objects to build query strings.

The current idea can be found in:

https://github.com/gslinker/solr/tree/SOLRJ_QUERY_BUILDER

Term and TermGroup are the foremost classes.

-----------------

Term is a unit of search. It is composed of two elements, the value of the word 
as a string and the name of the field.
For example:
+title:"pink panther"~1
title:("pink panther" "treasure island")
year:[1950 TO 1960]^=2
year:1953^0.5

Instantiate a Term and set the values and call toString to get a string that 
can be used in a Standard Solr Query.
       Term term = new Term("pink panther").withBoost(1.5f);
       term. toString()
 
       Output: "pink panther"^1.5
  
       Term term = new Term("title", "pink panther").withBoost(1.5f);
       term. toString()
 
       Output: title:"pink panther"^1.5
  


-----------------

TermGroup aggregates Terms and other TermGroups to form complex queries that 
can be used in a Standard Solr Query
Example:
          TermGroup group = new TermGroup().with(Occur. MUST).withBoost(1.4f);
          group. addTerm(new Term("foo", "bar").withProximity(1));
 
          String query = group. toString();
 
          Output: +( foo:bar~1 )^1.4
  
Example:
          TermGroup group = new TermGroup().withConstantScore(5.0f);
          group. addTerm(new Term("foo", "bar").withProximity(1));
 
          String query = group. toString();
 
          Output: ( foo:bar~1 )^=5
  
Instead of using string manipulation to create complex query strings the 
TermGroup allows complex queries to be built inside an object model that can be 
more easily changed.
If you need to generate a query like this:
  +(
        (
                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
  )
 
 
  The code to do so is as simple this:
 
      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));

-----------------


The classes do not depend on any Solr core or Lucene classes. Because of this 
there is a partial copy of the Lucene Occur enum for SHOULD, MUST, and MUST_NOT.

There are fluent style builders using the pattern "with" and there are the 
expected Java getters and setters.

Before I make an official Pull Request I would like any feedback. Please check 
out my branch and experiment with the code. If this is something the community 
agrees that it would be a good addition to SolrJ then I will continue the 
process of contributing this code.

If this is not needed please let me know.

This code is a subset of code that I use daily that has evolved over several 
years. I have re-implemented only the most basic functionality.

Geoffrey Slinker

  
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@solr.apache.org
For additional commands, e-mail: dev-h...@solr.apache.org

Reply via email to