2011-07-24 66 views
2

我正在使用Lucene 3.3.0和java。我面臨以下問題,不知道是否有解決方案。Lucene全文搜索

我使用StandardAnalyzer對以下文本進行索引:「男孩打得這麼辛苦贏得比賽」,然後我使用「play」進行查詢搜索......只有當我使用WildcardQuery構建器時,Lucene纔會發現點擊。

問題是,當我嘗試搜索「男孩遊戲」時,發現根本沒有命中。

有沒有辦法讓Lucene製作類似上下文搜索的東西來解決這個問題?

感謝, 薩默爾

private static void addDoc(IndexWriter w, String value) throws IOException { 
    Document doc = new Document(); 
    doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED)); 
    w.addDocument(doc); 
} 

@SuppressWarnings("deprecation") 
public static void lucene(String args, String query) throws IOException, ParseException { 
    // 0. Specify the analyzer for tokenizing text. 
    // The same analyzer should be used for indexing and searching 
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); 

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

    // the boolean arg in the IndexWriter ctor means to 
    // create a new index, overwriting any existing index 
    IndexWriter w = new IndexWriter(index, analyzer, true, 
      IndexWriter.MaxFieldLength.UNLIMITED); 
    String[] splitOnLinefeed = args.split("\n"); 
    for (int i = 0; i < splitOnLinefeed.length; i++) { 
     addDoc(w, splitOnLinefeed[i]); 
    } 
    w.close(); 

    // 2. query 
    String querystr = query+"*"; 

    // the "title" arg specifies the default field to use 
    // when no field is explicitly specified in the query. 
    Query q = new QueryParser(Version.LUCENE_CURRENT, "title", analyzer) 
      .parse(querystr); 

    // 3. search 
    IndexSearcher searcher = new IndexSearcher(index, true); 
    ScoreDoc[] hits = searcher.search(q, 100).scoreDocs; 

    // 4. display results 
    System.out.println("Found " + hits.length + " hit(s)."); 
    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("title")); 
    } 

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

public static void main(String[] args) throws Exception { 
    lucene(parse("Test.pdf"), "boy game"); 
} 
+0

請向我們顯示您的代碼。 – skaffman

+0

@Skaffman,我添加了代碼,我使用Tika來解析包含數據的PDF文件。 –

回答

0

1)查詢 「玩」:StandardAnalyzer不提供而產生。很顯然,你必須使用通配符或提供完全相同的術語。所以,沒有詞幹,「玩」和「玩」是完全不同的。

如果你想「的稱號:打」工作,您可以通過組合組件StandardAnalyzer的(標記生成器,過濾器)和PorterStemFilter

2)「男孩遊戲」創建您自己的分析:你確定是否您的pdf正在被正確解析。請嘗試將「args」參數打印到lucene();