You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Maria Vazquez <ma...@dexone.com> on 2011/10/12 20:01:12 UTC

Lucene queries to Solr requestHandler

I have these queries in Lucene 2.9.4, is there a way to convert these
exactly to Solr 3.4 but using only the solrconfig.xml? I will figure out the
queries but I wanted to know if it is even possible to go from here to
having something like this:

  <requestHandler name="/custom" class="solr.SearchHandler">
    ... queries
  </requestHandler>

So the front end just calls /custom?q=what and the requestHandler will build
the queries accordingly.
I don¹t want to put any of this logic in the front end, so I want to package
a requestHandler to do exactly what it was done in Lucene.
I have a lot of these type of queries built in Lucene that I have to migrate
to Solr, I don¹t want to write a different query parser plugin for each of
those.

Thanks in advance,
Maria


Example Lucene queries:


String tmpQuery1 = what.toLowerCase().trim();

Query wildcardkeyword_atts = new PrefixQuery(new Term("keyword_atts_exact",
tmpQuery1));
Query wildcardkeyword = new PrefixQuery(new Term("keyword_exact",
tmpQuery1));

BooleanQuery keywordExactQuery = new BooleanQuery();
keywordExactQuery.setBoost(10);
keywordExactQuery.add(new TermQuery(new Term("keyword_atts_exact",
tmpQuery1)), BooleanClause.Occur.SHOULD);
keywordExactQuery.add(new TermQuery(new Term("keyword_exact", tmpQuery1)),
BooleanClause.Occur.SHOULD);

BooleanQuery bkeywordAtts = new BooleanQuery();
PhraseQuery pPhraseKeywordAtt = new PhraseQuery();
pPhraseKeywordAtt.setSlop(2);

StringReader r2 = new StringReader(what);
TokenStream stream3 = analyzer.tokenStream(null, r2);
TermAttribute termAtt = (TermAttribute)
stream3.addAttribute(TermAttribute.class);
while (stream3.incrementToken()) {
    String sTerm = termAtt.term().trim();
    TermQuery tKeywordAtts = new TermQuery(new Term("keyword_atts", sTerm));
    pPhraseKeywordAtt.add(new Term("keyword_atts", sTerm));
    bkeywordAtts.add(tKeywordAtts, BooleanClause.Occur.MUST);
}

BooleanQuery m_mainQuery = new BooleanQuery();
m_mainQuery.add(wildcardkeyword_atts, BooleanClause.Occur.SHOULD);
m_mainQuery.add(wildcardkeyword, BooleanClause.Occur.SHOULD);
m_mainQuery.add(keywordExactQuery, BooleanClause.Occur.SHOULD);
m_mainQuery.add(pPhraseKeywordAtt, BooleanClause.Occur.SHOULD);

BooleanQuery m_finalQuery = new BooleanQuery();
m_finalQuery.add(new TermQuery(new Term("typename", "industry.category")),
BooleanClause.Occur.MUST);
m_finalQuery.add(m_mainQuery, BooleanClause.Occur.MUST);

Re: Lucene queries to Solr requestHandler

Posted by Chris Hostetter <ho...@fucit.org>.
Grrr.... cut/paste mistake.

This...

:   public class FieldQParserPlugin extends QParserPlugin {

...should have been something like...

  public class MyQParserPlugin extends QParserPlugin {

...to match the configuration example...

  <queryParser name="customQP" class="com.mycompany.MyQParserPlugin"/>


-Hoss

Re: Lucene queries to Solr requestHandler

Posted by Chris Hostetter <ho...@fucit.org>.
: I have these queries in Lucene 2.9.4, is there a way to convert these
: exactly to Solr 3.4 but using only the solrconfig.xml? I will figure out the
: queries but I wanted to know if it is even possible to go from here to
: having something like this:
: 
:   <requestHandler name="/custom" class="solr.SearchHandler">
:     ... queries
:   </requestHandler>
: 
: So the front end just calls /custom?q=what and the requestHandler will build
: the queries accordingly.

the simplest way to go about something like this is to implement a 
QParserPlugin for each little bit of custom logic you have for building a 
query from user input.  then register each QParserPlugin with a name, and 
configure a handler instance with an invariant "defType" set to that name.

so for example: assume all the java code you mentioned in your email to 
build up a query was implemented in a method you had with a signature that 
looked like this...

   public static Query myQueryParserHelper(final String userInput)

...you would wrap that method in a QParserPlugin like so...

  public class FieldQParserPlugin extends QParserPlugin {
    public void init(NamedList args) {
    }
    @Override
    public QParser createParser(String qstr, SolrParams localParams,
                                SolrParams params, SolrQueryRequest req) {
      return new QParser(qstr, localParams, params, req) {
        @Override
        public Query parse() throws ParseException {
          String queryText = localParams.get(QueryParsing.V);
	  return myQueryParserHelper(queryText);
        }
      };
    }
  }

...and then your configuration would look something like...

  <queryParser name="customQP" class="com.mycompany.MyQParserPlugin"/>
  <requestHandler name="/custom" class="solr.SearchHandler">
    <lst name="invariants">
      <str name="defType">customQP</str>
    </lst>
  </requestHandler

-Hoss