0

我试图创建一个 CustomQueryParser,我也在其中使用 OpenNLP 库。

我的目标是,如果我有一个疑问“有多少有缺陷的轮辋导致中国的 ABC 轮胎出现故障”

我希望最终查询类似于“有缺陷的轮辋故障轮胎中国”,然后将转到分析器进行进一步处理。

这是我的 QueryParserPlugin 代码 -

package com.mycompany.lucene.search;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QParserPlugin;
import com.mycompany.lucene.search.QueryParser;

public class QueryParserPlugin extends QParserPlugin {
@Override
  public QParser createParser(String qstr, SolrParams localParams, 
SolrParams params, SolrQueryRequest req) {
    return new QueryParser(qstr, localParams, params, req, "body_txt_str");
  }
}

还有我的 QueryParser 的代码 -

package com.mycompany.lucene.search;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.QParser;
import org.apache.solr.search.SyntaxError;
import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;

public class QueryParser extends QParser {

  private String fieldName;

public QueryParser(String qstr, SolrParams localParams, SolrParams params, 
SolrQueryRequest req,
      String defaultFieldName) {

    super(qstr, localParams, params, req);

    fieldName = localParams.get("field");
    if (fieldName == null) {
      fieldName = params.get("df");
    }
  }
@Override
  public Query parse() throws SyntaxError {
    Analyzer analyzer = req.getSchema().getQueryAnalyzer(); 
    InputStream tokenModelIn = null;
    InputStream posModelIn = null;
    try {
        tokenModelIn = new FileInputStream("/Files/en-token.bin");
     } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    TokenizerModel tokenModel = null;
    try {
        tokenModel = new TokenizerModel(tokenModelIn);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Tokenizer tokenizer = new TokenizerME(tokenModel);
    String tokens[] = tokenizer.tokenize(qstr);

    try {
        posModelIn = new FileInputStream("/Files/en-pos-maxent.bin");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
         e.printStackTrace();
    }
      // loading the parts-of-speech model from stream
    POSModel posModel = null;
    try {
         posModel = new POSModel(posModelIn);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // initializing the parts-of-speech tagger with model 
    POSTaggerME posTagger = new POSTaggerME(posModel);
    // Tagger tagging the tokens
    String tags[] = posTagger.tag(tokens);
    String final_query = "";
    for(int i=0;i<tokens.length;i++){
        if (tags[i]=="JJ" || tags[i]=="NNS" || tags[i]=="NN") {
            final_query = final_query + " " +tokens[i];
        }
    }
    TermQuery tq= new TermQuery(new Term(fieldName,final_query));  
    return tq; 
    }
}

然后我将其导出为 jar 并将这些 jar 添加到我的 solrconfig.xml -

<lib dir="${solr.install.dir:../../../..}/contrib/customparser/lib" 
 regex=".*\.JAR" />
<lib dir="${solr.install.dir:../../../..}/contrib/analysis-extras/lib" 
 regex="opennlp-.*\.jar" />

但得到以下错误:

造成的:

java.lang.NoClassDefFoundError: opennlp/tools/tokenize/Tokenizer
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:541)
    at org.apache.solr.core.SolrResourceLoader.findClass(SolrResourceLoader.java:488)
    at org.apache.solr.core.SolrCore.createInstance(SolrCore.java:786)
    at org.apache.solr.core.PluginBag.createPlugin(PluginBag.java:135)
    at org.apache.solr.core.PluginBag.init(PluginBag.java:271)
    at org.apache.solr.core.PluginBag.init(PluginBag.java:260)
    at org.apache.solr.core.SolrCore.<init>(SolrCore.java:957)
    ... 9 more

这是我第一次创建 CustomQueryParser,请您帮帮我。

谢谢

4

1 回答 1

1

很可能是你的道路

${solr.install.dir:../../../..}/contrib/analysis-extras/lib

不包含相关的 opennlp jar 或正则表达式不合适。这是首先要检查的。

您必须“捆绑”自定义查询解析器 jar 中的 opennlp 依赖项(例如,如果您使用 maven 构建项目,使用 maven-assembly-plugin、maven-shade-plugin 等)或确保 opennlp 特定solrconfig.xml 中相关指令中的 jar 匹配。

于 2018-04-11T06:35:02.593 回答