0

Javaでlucene 4.0を使用しています。文字列内の文字列を検索しようとしています。lucene hello world の例を見ると、「inLuceneAction」という語句内に「lucene」というテキストが含まれていることがわかります。この場合、1 つではなく 2 つの一致を見つけてもらいたいのです。

それを行う方法についてのアイデアはありますか?

ありがとう

public class HelloLucene {
 public static void main(String[] args) 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_40);

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

IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_40, analyzer);

IndexWriter w = new IndexWriter(index, config);
addDoc(w, "inLuceneAction", "193398817");
addDoc(w, "Lucene for Dummies", "55320055Z");
addDoc(w, "Managing Gigabytes", "55063554A");
addDoc(w, "The Art of Computer Science", "9900333X");
w.close();

// 2. query
String querystr = args.length > 0 ? args[0] : "lucene";

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

// 3. search
int hitsPerPage = 10;
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;

// 4. display results
System.out.println("Found " + hits.length + " hits.");
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("isbn") + "\t" + d.get("title"));
}
// reader can only be closed when there
// is no need to access the documents any more.
reader.close(); 
}
private static void addDoc(IndexWriter w, String title, String isbn) throws IOException {
Document doc = new Document();
doc.add(new TextField("title", title, Field.Store.YES));

// use a string field for isbn because we don't want it tokenized
doc.add(new StringField("isbn", isbn, Field.Store.YES));
w.addDocument(doc);
}
}
4

1 に答える 1

1

デフォルトの方法で用語にインデックスを付ける場合、意味は 1 つの用語であり、Lucene はこの用語に異なる接頭辞を持っているため、指定inLuceneActionできません。この文字列を分析して、インデックス付きの 3 つの用語が得られるようにします。次に、それを取得します。このための既製のアナライザーを見つけるか、独自のアナライザーを作成する必要があります。独自のアナライザーを作成することは、単一の StackOverflow 回答の範囲外ですが、開始するのに最適な場所は、org.apache.lucene.analysisパッケージの Javadoc ページの下部にあるパッケージ情報です。seekLucenein Lucene Action

于 2013-01-17T09:47:30.207 に答える