【问题标题】:Lucene 4.0 in text search文本搜索中的 Lucene 4.0
【发布时间】:2013-01-17 09:18:32
【问题描述】:

我正在使用带有 java 的 lucene 4.0。我正在尝试在字符串中搜索字符串。如果我们查看 lucene hello world 示例,我希望在短语“inLuceneAction”中找到文本“lucene”。在这种情况下,我希望它为我找到两个匹配项,而不是一个。

关于如何做到这一点的任何想法?

谢谢

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);
}
}

【问题讨论】:

    标签: java lucene


    【解决方案1】:

    如果您以默认方式索引术语,即inLuceneAction 是一个术语,Lucene 将无法在给定Lucene 的情况下将seek 指向该术语,因为它具有不同的前缀。分析此字符串,使其产生三个索引项:inLuceneAction,然后您将获取它。您要么为此找到现成的分析器,要么必须编写自己的分析器。编写自己的分析器有点超出了 StackOverflow 答案的范围,但一个很好的起点是 org.apache.lucene.analysis 包 Javadoc 页面底部的包信息。

    【讨论】:

    • 感谢您的快速回答。我正在尝试进行简单的 url 搜索。我无法分析索引词(它没有结构)。那么进行 url 搜索的最佳方法是什么?
    • 然后你必须索引所有子字符串:inLuceneActionnLuceneActionLuceneAction,等等。我认为对这种索引/查询有内置支持,但我不记得了。
    • 在 3.0 版本中似乎更容易了。 stackoverflow.com/questions/5321388/lucene-search-by-url
    • 不,这个答案没有解决任何问题;它只是避免了QueryParser 并使用SHOULD 约束,这意味着根本没有MUST 术语;查询将返回 所有 文档,但如果某些文档恰好与 URL 完全匹配,它们将位于顶部。
    • @femtoRgon 关于EdgeNGramTokenizer,我想OP应该使用Side.BACK并指定足够大的最大n-gram大小。
    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2012-09-25
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    相关资源
    最近更新 更多