lucene-solr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "tom liu (JIRA)" <j...@apache.org>
Subject [jira] Created: (SOLR-1422) Add one QParser to Query Chinese Sentences
Date Fri, 11 Sep 2009 09:58:57 GMT
Add one QParser to Query Chinese Sentences
------------------------------------------

                 Key: SOLR-1422
                 URL: https://issues.apache.org/jira/browse/SOLR-1422
             Project: Solr
          Issue Type: Improvement
          Components: search
         Environment: windows xp/ jdk1.6 / tomcat6
            Reporter: tom liu


DisMaxQParser do not correctly analysis chinese sentence. So, i implement one QParser derived
from DisMax.
Limis:
     in schema.xml, set defaultSearchField to chineseFieldType-Field
Result:
     if you input C1C2C3C4, then:
     in DisMaxQParser, we will find that qstr is "C1C2 C3C4"

1. SentenceDisMaxQParser Class::
package org.apache.solr.search;

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Token;
import java.io.StringReader;

import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.DefaultSolrParams;
import org.apache.solr.common.params.DisMaxParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.util.SolrPluginUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SentenceDisMaxQParser extends DisMaxQParser {
  private static Logger log = LoggerFactory.getLogger(SentenceDisMaxQParser.class);
  public SentenceDisMaxQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest
req) {
    super(qstr, localParams, params, req);

    Analyzer analyzer = req.getSchema().getQueryAnalyzer();
	if(null == analyzer)
		return;

	StringBuilder norm = new StringBuilder();
log.info("before analyzer, qstr="+this.qstr);
	try{
		TokenStream tokens = analyzer.reusableTokenStream( req.getSchema().getDefaultSearchFieldName(),
new StringReader( this.qstr ) );
		tokens.reset();
	    Token token = tokens.next();
	    while( token != null ) {
	      norm.append( new String(token.termBuffer(), 0, token.termLength()) ).append(" ");
	      token = tokens.next();
	    }
	} catch(Exception ex){
log.info("Ex="+ex);
	}
	if(norm.length() > 0)
		this.qstr = norm.toString();
log.info("after analyzer, qstr="+this.qstr);
  }
}

2. SentenceDisMaxQParserPlugin Class::
package org.apache.solr.search;

import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;

public class SentenceDisMaxQParserPlugin extends QParserPlugin {
  public static String NAME = "sdismax";

  public void init(NamedList args) {
  }

  public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest
req) {
    return new SentenceDisMaxQParser(qstr, localParams, params, req);
  }
}


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message