【问题标题】:Make field case insensitive in lucene在 lucene 中使字段不区分大小写
【发布时间】:2014-04-23 15:59:17
【问题描述】:

如何在 lucene 中使字段不区分大小写?假设,我有以下文件:

用户:xyz

现在,文档应该作为查询“user:xyz”、“uSer:xyz”或“usEr:xyz”的结果返回。

可能的解决方案是在索引和搜索时将字段小写。但是在检索文档时我需要该字段的确切值。另外,另一种解决方案是对该字段进行两次索引,但这也不是正确的解决方案。

这是 lucene 示例。当查询为“user:xyz”时,文档不匹配。但是,如果我使用查询“用户:xyz”,那么文档匹配,因为在索引时我有字段为“用户”。

public void testFieldCaseSensitive() throws ParseException,
        QueryNodeException {
    StandardQueryParser parser = new StandardQueryParser();
    Query luceneQuery = parser.parse("user:xyz","");
    MemoryIndex memoryIndex = new MemoryIndex();
    memoryIndex.addField("User", "xyz", new StandardAnalyzer(
            Version.LUCENE_43));
    memoryIndex.search(luceneQuery);
    Assert.assertTrue(memoryIndex.search(luceneQuery) > 0);
}

【问题讨论】:

  • 如果我没记错的话,我猜它已经不区分大小写了?
  • 你能提供一些例子吗?我已经处理了这些值,但没有处理字段本身。
  • 是的,马上看到我的回答。
  • 对不起,我第一次阅读时没有正确理解您的问题。应该更仔细地阅读。

标签: java solr lucene information-retrieval


【解决方案1】:

字段名称区分大小写。据我所知,没有开关可以翻转以使其成为其他方式。

可能最合理的方法是确保在索引文档时,所有字段名称都是小写的。然后,在查询时,如果您不针对任何区分大小写的字段进行查询,您可以使用String.toLowercase(),将整个查询字符串也变为小写,从而有效地使其不区分大小写。

【讨论】:

  • 我自己做了这个,效果很好——用户真的很喜欢它。
【解决方案2】:

Apache Lucene 已经不区分大小写,无论您搜索什么(区分大小写或不区分大小写),它都会为您带来结果。

基本上,您使用的索引已经涵盖了它,并且在大多数情况下它是 StandardAnalyzer。我刚刚测试过了。

搜索:

DocSearchEngine searcher = new DocSearchEngine();
ScoreDoc[] hits = searcher.searchIndexWithQueryParser("SeArch TeXT");
List<ResStructure> resultSet = searcher.printResultList(hits);

对于索引:

writer = new IndexWriter(FSDirectory.open(new File(indexDir)),
    new IndexWriterConfig(Version.LUCENE_45 ,new StandardAnalyzer(Version.LUCENE_45)));

【讨论】:

  • 我现在在我的帖子中添加了 lucene 代码。我指的不是值,而是字段。能否请您看一下代码并发表评论?
  • 我查看了您的代码,我知道您在谈论价值。请添加 luceneQuery.setAllowLeadingWildcard( true );它绕过您选择的分析仪并为您提供结果。见链接wiki.apache.org/lucene-java/…
  • 我试过 parser.setAllowLeadingWildcard(true) 但还是不行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-16
  • 2011-08-28
  • 2015-10-21
相关资源
最近更新 更多