【问题标题】:How to index a String in Lucene?如何在 Lucene 中索引字符串?
【发布时间】:2014-02-16 07:33:51
【问题描述】:

我正在使用 Lucene 来索引我从文档中读取的字符串。我没有使用阅读器类,因为我需要将字符串索引到不同的字段。

document.add(new Field("FIELD1","string1", Field.Store.YES, Field.Index.UNTOKENIZED));
document.add(new Field("FIELD2","string2", Field.Store.YES, Field.Index.UNTOKENIZED));

这适用于建立索引但搜索

QueryParser queryParser = new QueryParser("FIELD1", new StandardAnalyzer());
Query query = queryParser.parse(searchString);
Hits hits = indexSearcher.search(query);
System.out.println("Number of hits: " + hits.length());

不返回任何结果。

但是当我索引一个句子时,

document.add(new Field("FIELD1","This is sentence to be indexed", Field.Store.YES, Field.Index.TOKENIZED));

搜索工作正常。

谢谢。

【问题讨论】:

    标签: java lucene indexing search-engine


    【解决方案1】:

    您还需要将带有单词的字段的参数设置为 Field.Index.TOKENIZED,因为只有在标记化时才能进行搜索。单词“string1”将被索引为“string1”。如果没有标记化,它根本不会被索引。

    使用这个:

    document.add(new Field("FIELD1","string1", Field.Store.YES, Field.Index.TOKENIZED));
    document.add(new Field("FIELD2","string2", Field.Store.YES, Field.Index.TOKENIZED));
    

    当您想要索引包含多个单词的字符串时,例如“两个词”作为一个可搜索元素而不标记为 2 个词,您需要在索引期间使用 KeywordAnalyzer 将整个字符串作为标记,或者您可以在较新版本的 Lucene 中使用 StringField 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-27
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 2016-02-18
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多