2016-03-18 67 views
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

在先進的感謝

回答

1

如果您想要匹配領域的確切的,未經修改的和未經確認的價值,你根本不應該分析它。只需使用StringField而不是TextField

如果您想要進行某些分析(即縮小等),但沒有標記,則可以在Analyzer實現中使用KeywordTokenizer

如果您使用QueryParser來創建查詢,請注意解析器如何使用空格來分隔查詢子句。你可能會發現有必要寫下如下的查詢:city:ANDHRA\ PRADESH(我做不是認爲QueryParser.escape會爲你做這個)。