2
我創建了一個Lucene 4.10.3索引。匹配lucene整個字段的確切值
我正在使用他的StandardAnalyzer。
String indexpath="C:\\TEMP";
IndexWriterConfig iwc=newIndexWriterConfig(Version.LUCENE_4_10_3,new StandardAnalyzer(CharArraySet.EMPTY_SET));
Directory dir = FSDirectory.open(new File(indexpath));
IndexWriter indexWriter = new IndexWriter(dir, iwc);
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
Document doc = new Document();
doc.add(new TextField("city", "ANDHRA",Store.YES));
doc.add(new TextField("city", "ANDHRA PRADESH",Store.YES));
doc.add(new TextField("city", "ASSAM AND NAGALAND",Store.YES));
doc.add(new TextField("city", "ASSAM",Store.YES));
doc.add(new TextField("city", "PUNJAB",Store.YES));
doc.add(new TextField("city", "PUNJAB AND HARYANA",Store.YES));
indexWriter.addDocument(doc);
當我嘗試在Lucene索引搜索使用短語查詢
例如
try {
QueryBuilder build=new QueryBuilder(new KeywordAnalyzer());
Query q1=build.createPhraseQuery("city","ANDHRA");
Directory dir = FSDirectory.open(new File("C:\\TEMP"));
DirectoryReader indexReader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(indexReader);
ScoreDoc hits[] = searcher.search(q1,10).scoreDocs;
Set<String> set=new HashSet<String>();
set.add("city");
for (int i=0; i < hits.length; i++) {
Document document = indexReader.document(hits[i].doc,set);
System.out.println(document.get("city"));
}
} catch (IOException e) {
e.printStackTrace();
}
我們得到的結果作爲後續
ANDHRA
安德拉邦
當我搜索「ANDHRA」如何獲得唯一的「ANDHRA」的結果, 不是「安得拉邦」,如何匹配Lucene的整個領域。通過採用StandardAnalyzer
在先進的感謝