我目前正在尝试创建一个查询,我可以搜索4个字段,但返回与任何字段匹配的文档。
有人可以告诉我如何设置它。我尝试过BooleanQuery和MultiFieldQueryParser,但我无法获得任何点击。
BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("field1", text1));
Query query2 = new TermQuery(new Term("field2", text2));
Query query3 = new TermQuery(new Term("field3", text3));
Query query4 = new TermQuery(new Term("field4", text4));
booleanQuery.add(query1, Occur.SHOULD);
booleanQuery.add(query2, Occur.SHOULD);
booleanQuery.add(query3, Occur.SHOULD);
booleanQuery.add(query4, Occur.SHOULD);
MultiFieldQueryParser multiQueryParser = new MultiFieldQueryParser(Version.LUCENE_44,
new String[] {"field1", "field2", "field3", "field4"}, analyzer);
multiQueryParser.setDefaultOperator(Operator.OR);
我使用multiQueryParser获取结果,运算符设置为AND但是我没有得到OR的任何内容。我认为只有在匹配2个字段时才会返回结果。
有人可以说出错是什么意思吗?
由于
另外,我要寻找部分匹配或完全匹配。
完整代码:
公共课演示{
IndexWriter indexWriter = null;
public boolean openIndex() {
try {
Directory dir = FSDirectory.open(new File(indexerPath));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
indexWriter = new IndexWriter(dir, iwc);
isIndexOpen = true;
return true;
} catch (Exception e) {
return false;
}
}
public void finish() {
try {
indexWriter.close();
isIndexOpen = false;
} catch (IOException e) {
}
}
@Override
public void addDoc(MyObject obj) {
Document doc = new Document();
doc.add(new TextField("field1", obj.getField1(), Field.Store.YES));
doc.add(new TextField("field2", obj.getField2(),, Field.Store.YES));
doc.add(new TextField("field3", obj.getField3(),, Field.Store.YES));
doc.add(new TextField("field4", obj.getField4(),, Field.Store.YES));
try {
indexWriter.addDocument(doc);
} catch (IOException e) {
}
}
@Override
public void getResults(String text) throws ParseException, IOException {
Directory dir = FSDirectory.open(new File(indexerPath));
IndexReader reader = DirectoryReader.open(dir);
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
MultiFieldQueryParser multiQueryParser = new MultiFieldQueryParser(Version.LUCENE_44,
new String[] {"field1", "field2", "field3", "field4"}, analyzer);
multiQueryParser.setDefaultOperator(Operator.OR);
Query query = multiQueryParser.parse(text);
searcher.search(query, collector);
hits = collector.topDocs().scoreDocs;
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
Log.info("\f1: "+d.get("field1") + " f2: " + d.get("field2") +
" f3: "+d.get("field3") + " f4: "+ d.get("field3"));
}
return results;
}
}
答案 0 :(得分:3)
自包含示例表示编译并依赖于无外部资源来运行/演示问题的代码。你的代码没有。
以下是代码的修改版本,可以使用并演示部分匹配。
public class Demo {
static Directory dir;
IndexWriter indexWriter;
public boolean openIndex() {
try {
dir = new RAMDirectory();
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
indexWriter = new IndexWriter(dir, iwc);
return true;
} catch (Exception e) {
return false;
}
}
public void finish() {
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void addDoc() {
Document doc = new Document();
doc.add(new TextField("field1", "val1", Field.Store.YES));
doc.add(new TextField("field2", "val2", Field.Store.YES));
doc.add(new TextField("field3", "val3", Field.Store.YES));
doc.add(new TextField("field4", "val4", Field.Store.YES));
try {
indexWriter.addDocument(doc);
indexWriter.commit();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void printResults(Query query) throws IOException {
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs result = searcher.search(query, 5);
System.out.println("# of results:" + result.totalHits);
for (ScoreDoc doc : result.scoreDocs) {
System.out.println(doc);
}
reader.close();
}
public static void main(String[] args) throws IOException, ParseException {
Demo demo = new Demo();
demo.openIndex();
demo.addDoc();
demo.finish();
BooleanQuery booleanQuery = new BooleanQuery();
Query query1 = new TermQuery(new Term("field1", "val1"));
Query query2 = new TermQuery(new Term("field2", "val2"));
Query query3 = new TermQuery(new Term("field3", "val3"));
Query query4 = new TermQuery(new Term("field4", "val4"));
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);
booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
booleanQuery.add(query3, BooleanClause.Occur.SHOULD);
booleanQuery.add(query4, BooleanClause.Occur.SHOULD);
printResults(booleanQuery);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
MultiFieldQueryParser multiQueryParser = new MultiFieldQueryParser(Version.LUCENE_44,
new String[] {"field1", "field2", "field3", "field4"}, analyzer);
multiQueryParser.setDefaultOperator(QueryParser.Operator.OR);
Query mfQuery = multiQueryParser.parse("field1:val1 field2:val2 field3:val3 field4:val4");
printResults(mfQuery);
Query mfQuery2 = multiQueryParser.parse("field1:val* field2:x field3:y field4:z");
printResults(mfQuery2);
}
}