我使用Lucene 5.3并尝试使用queryparser-syntax搜索多个字段。我在Lucene Tutorials中找到了一个简短的例子,并将其修改为5.3版并搜索这些字段。
package lucenewriterexample;
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.ParseException;
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 java.io.IOException;
public class LuceneWriterExample {
public static void main(String[] args) throws IOException, ParseException {
StandardAnalyzer analyzer = new StandardAnalyzer();
Directory index = new RAMDirectory();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
try (IndexWriter writer = new IndexWriter(index, config)) {
addDoc(writer, "Day first : Lucence Introduction test.", "3436NRX");
addDoc(writer, "Day second , part one : Lucence Projects.", "3437RJ1");
addDoc(writer, "Day second , part two: Lucence Uses testing rr.", "3437RJ2");
addDoc(writer, "Day third : Lucence Demos.", "34338KRX");
}
String querystr = "title:(part) AND course_code:(3437RJ1)";
Query q = new QueryParser("title", analyzer).parse(querystr);
// 3. searching
int hitsPerPage = 10;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// 4. display results
System.out.println("Query string: " + querystr );
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("course_code") + "\t" + d.get("title"));
}
// Finally , close reader
}
private static void addDoc(IndexWriter w, String title, String courseCode) throws IOException {
Document doc = new Document();
doc.add(new TextField ("title", title, Field.Store.YES));
doc.add(new StringField("course_code", courseCode, Field.Store.YES));
w.addDocument(doc);
}
queryparser正在为“title:part”工作,然后我得到所有包含“part”的文档,但如果我使用“title :( part)AND course_code:(3437RJ1)”或“title :( part)OR course_code :(3436NRX)结果为0。 搜索中的错误在哪里?
答案 0 :(得分:0)
我跑了这个并试了title:(part) OR course_code:(3436NRX)
,我得到了2个结果,正如我所料。也许你的意思是你期望第三个结果匹配course_code,但没有得到它。如果您真的意味着您的查询实际上得到了零结果,我不确定问题是什么。
那么,为什么你不能在course_code上获得匹配?
与lucene一样常见的情况是,您的分析仪不匹配。您的queryparser正在使用StandardAnalyzer
,但course_code是StringField
,因此根本没有进行分析。 StandardAnalyzer包含一个小写所有内容的过滤器,因此最终结果是您有一个3436NRX
字段和course_code:3436nrx
的查询。
可能的解决方案是:
TermQuery
代替StringField
TextField
等