2011-07-24 26 views
2

私はLucene 3.3.0をjavaで使用しています。私は次の問題に直面しており、解決策が存在するかどうかは分かりません。Luceneフルテキスト検索

私はStandardAnalyzerを使用して "ゲームに勝つために大変なプレイをした"少年は "play"を使ってクエリを検索します... LuceneはWildcardQuery Builderを使用するときのみヒットします。

問題は、「少年のゲーム」を検索しようとすると、全くヒットしないということです。

この問題を解決するために、Luceneがコンテキスト検索のようなものを作ることはありませんか?

おかげで、 SAMER

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、データを含むPDFファイルを解析するためにTikaを使用してコードを追加しました。 –

答えて

0

1)クエリ "遊び":StandardAnalyzerステミング提供されていません。ワイルドカードを使用するか、まったく同じ用語を指定する必要があることは明らかです。だから、ステムミングがなければ、「遊び」と「遊ぶ」は全く違っています。

あなたが「タイトル:プレイ」したい場合は

2を使用すると、StandardAnalyzerのコンポーネント(トークナイザ、フィルタ)を組み合わせることにより、独自のアナライザーを作成することができ、動作するようにとPorterStemFilter)「少年ゲーム」:あなたはあなたのかどうかを確認してくださいましたpdfが正しく解析されています。 lucene()に "args"引数を出力してみてください。