Parsing 无法解析导入org.apache.lucene.queryparser

Parsing 无法解析导入org.apache.lucene.queryparser,parsing,indexing,lucene,Parsing,Indexing,Lucene,我正在使用Lucene 6.6,在导入Lucene.queryparser时遇到了困难,我确实检查了Lucene文档,但它现在不存在。 我正在使用下面的代码。lucene6中的queryparser有其他选择吗 import java.io.IOException; import java.text.ParseException; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.luc

我正在使用Lucene 6.6,在导入Lucene.queryparser时遇到了困难,我确实检查了Lucene文档,但它现在不存在。 我正在使用下面的代码。lucene6中的queryparser有其他选择吗

import java.io.IOException;
import java.text.ParseException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.queryparser.classic.QueryParser;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class HelloLucene {

    public static void main(String[] args) throws IOException, ParseException {
        // 0. Specify the analyzer for tokenizing text.
        //    The same analyzer should be used for indexing and searching
        StandardAnalyzer analyzer = new StandardAnalyzer();

        // 1. create the index
        Directory index = new RAMDirectory();

        IndexWriterConfig config = new IndexWriterConfig(analyzer);

        IndexWriter w = new IndexWriter(index, config);
        addDoc(w, "Lucene in Action", "193398817");
        addDoc(w, "Lucene for Dummies", "55320055Z");
        addDoc(w, "Managing Gigabytes", "55063554A");
        addDoc(w, "The Art of Computer Science", "9900333X");
        w.close();

        // 2. query
        String querystr = args.length > 0 ? args[0] : "lucene";

        // the "title" arg specifies the default field to use
        // when no field is explicitly specified in the query.
        Query q = null;
        try {
            q = new QueryParser(Version.LUCENE_6_6_0, "title", analyzer).parse(querystr);
        } catch (org.apache.lucene.queryparser.classic.ParseException e) {
            e.printStackTrace();
        }

        // 3. search
        int hitsPerPage = 10;
        IndexReader reader = DirectoryReader.open(index);
        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        searcher.search(q, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;

        // 4. display results
        System.out.println("Found " + hits.length + " hits.");
        for (int i = 0; i < hits.length; ++i) {
            int docId = hits[i].doc;
            Document d = searcher.doc(docId);
            System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
        }

        // reader can only be closed when there
        // is no need to access the documents any more.
        reader.close();
    }

    private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
        Document doc = new Document();
        doc.add(new TextField("title", title, Field.Store.YES));

        // use a string field for isbn because we don't want it tokenized
        doc.add(new StringField("isbn", isbn, Field.Store.YES));
        w.addDocument(doc);
    }
}
import java.io.IOException;
导入java.text.ParseException;
导入org.apache.lucene.analysis.standard.StandardAnalyzer;
导入org.apache.lucene.document.document;
导入org.apache.lucene.document.Field;
导入org.apache.lucene.document.StringField;
导入org.apache.lucene.document.TextField;
导入org.apache.lucene.index.DirectoryReader;
导入org.apache.lucene.index.IndexReader;
导入org.apache.lucene.index.IndexWriter;
导入org.apache.lucene.index.IndexWriterConfig;
导入org.apache.lucene.queryparser.classic.queryparser;
导入org.apache.lucene.search.indexsearch;
导入org.apache.lucene.search.Query;
导入org.apache.lucene.search.ScoreDoc;
导入org.apache.lucene.search.TopScoreDocCollector;
导入org.apache.lucene.store.Directory;
导入org.apache.lucene.store.RAMDirectory;
导入org.apache.lucene.util.Version;
公共级HelloLucene{
公共静态void main(字符串[]args)引发IOException、ParseException{
//0.指定用于标记文本的分析器。
//应使用相同的分析器进行索引和搜索
StandardAnalyzer=新的StandardAnalyzer();
//1.创建索引
目录索引=新的RAMDirectory();
IndexWriterConfig配置=新的IndexWriterConfig(分析器);
IndexWriter w=新的IndexWriter(索引,配置);
addDoc(w,“Lucene在行动”,“193398817”);
addDoc(w,“用于假人的Lucene”,“55320055Z”);
addDoc(w,“管理千兆字节”,“55063554A”);
addDoc(w,“计算机科学的艺术”,“9900333X”);
w、 close();
//2.查询
字符串querystr=args.length>0?args[0]:“lucene”;
//“title”参数指定要使用的默认字段
//当查询中未显式指定字段时。
查询q=null;
试一试{
q=新的QueryParser(Version.LUCENE_6_6_0,“title”,analyzer).parse(querystr);
}catch(org.apache.lucene.queryparser.classic.parsee){
e、 printStackTrace();
}
//3.搜索
int hitsPerPage=10;
IndexReader=DirectoryReader.open(索引);
IndexSearcher search=新的IndexSearcher(阅读器);
TopScoreDocCollector=TopScoreDocCollector.create(hitsPerPage,true);
搜索者。搜索(q,收集器);
ScoreDoc[]hits=collector.topDocs().scoreDocs;
//4.显示结果
System.out.println(“Found”+hits.length+“hits.”);
对于(int i=0;i
谢谢

问题解决了。 最初,在构建路径中,只添加了Lucene-core-6.6.0,但Lucene-queryparser-6.6.0是一个单独的jar文件,需要单独添加。

问题得到了解决。 最初,在构建路径中,只添加了Lucene-core-6.6.0,但是Lucene-queryparser-6.6.0是一个单独的jar文件,需要单独添加