Java 如何在Lucene中获取以特定字母开头的记录

Java 如何在Lucene中获取以特定字母开头的记录,java,apache,solr,lucene,Java,Apache,Solr,Lucene,我有一个缓存的名称列表,存储到Lucene数据结构中。我想找一些名字以特定字母开头的人 例如: 我的名单如下。我将它们存储到name字段中 foo bar blabla foo foo2 bar test data 当我用name:f*搜索时,它返回foo-bar,foo2-bar和blabla-foo。它检查字段中的每个单词,并获得blablafoo。但是我需要让名字以f开头,它的第一个字母是f,而不是记录包含以f开头的单词,即使它们在句子的末尾 有什么想法吗 通配符搜索 Lucene支持在

我有一个缓存的名称列表,存储到Lucene数据结构中。我想找一些名字以特定字母开头的人

例如: 我的名单如下。我将它们存储到
name
字段中

foo bar
blabla foo
foo2 bar
test data
当我用
name:f*
搜索时,它返回
foo-bar
foo2-bar
blabla-foo
。它检查字段中的每个单词,并获得
blablafoo
。但是我需要让名字以
f
开头,它的第一个字母是
f
,而不是记录包含以
f
开头的单词,即使它们在句子的末尾

有什么想法吗

通配符搜索

Lucene支持在单个术语(而不是短语查询)中进行单字符和多字符通配符搜索

要执行单字符通配符搜索,请使用“?”符号

要执行多字符通配符搜索,请使用“*”符号

单字符通配符搜索查找与替换的单字符匹配的术语。例如,要搜索“文本”或“测试”,可以使用搜索:

泰特 多字符通配符搜索查找0个或更多字符。例如,要搜索测试、测试或测试仪,可以使用搜索:

试验*

例如,使用regex

RegexQuery query = new RegexQuery(newTerm("^a.*$"));


query.setRegexImplementation(new JavaUtilRegexCapabilities());

return searcher.search(query, null, 1000).totalHits;

示例代码:

        BasicConfigurator.configure();

        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

        // Store the index in memory:
        Directory directory = new RAMDirectory();
        // To store an index on disk, use this instead:
        // Directory directory = FSDirectory.open(new
        // File("./lucene/data"));
        IndexWriterConfig config = new IndexWriterConfig(
                Version.LUCENE_CURRENT, analyzer);
        IndexWriter iwriter;

        iwriter = new IndexWriter(directory, config);

        String[] words = { "Olimpia", "Cerro", "Olimpo", "Libertad",
                "Nacional", "Sol", "O'higgins", "Sao Paulo",
                "Oriente Petrolero", "Barrio Obrero", "B. Obrero" };

        for (String word : words) {
            Document doc = new Document();
            String text = word;
            doc.add(new Field("name", text, Field.Store.YES,
                    Field.Index.NOT_ANALYZED));

            // ,Field.Store.NO, Field.Index.NOT_ANALYZED
            iwriter.addDocument(doc);
        }

        iwriter.close();

        // Now search the index:

        logger.info("HelloLucene.main: query2 -----------");

        DirectoryReader ireader2 = DirectoryReader.open(directory);
        IndexSearcher isearcher2 = new IndexSearcher(ireader2);

        logger.info("HelloLucene.main: query2 -----------");
        RegexQuery query2 = new RegexQuery(new Term("name", "O.*"));
        query2.setRegexImplementation(new JavaUtilRegexCapabilities(
                JavaUtilRegexCapabilities.FLAG_CASE_INSENSITIVE));

        ScoreDoc[] hits2 = isearcher2.search(query2, null, 1000).scoreDocs;
        for (int i = 0; i < hits2.length; i++) {
            Document hitDoc = isearcher2.doc(hits2[i].doc);
            logger.info("HelloLucene.main: starting with O = "
                    + hitDoc.get("name"));

        }
BasicConfigurator.configure();
Analyzer Analyzer=新的StandardAnalyzer(当前版本为LUCENE_);
//将索引存储在内存中:
目录目录=新的RAMDirectory();
//要在磁盘上存储索引,请改用以下方法:
//Directory Directory=FSDirectory.open(新建
//文件(“./lucene/data”);
IndexWriterConfig配置=新建IndexWriterConfig(
版本。LUCENE_电流,分析仪);
索引作者iwriter;
iwriter=newindexwriter(目录,配置);
String[]words={“Olimpia”、“Cerro”、“Olimpo”、“Libertad”,
“国家”、“太阳”、“奥希金斯”、“圣保罗”,
“石油方向”、“巴里奥·奥布雷罗”、“B.奥布雷罗”};
for(字符串字:字){
单据单据=新单据();
字符串文本=单词;
添加文档(新字段(“名称”,文本,Field.Store.YES,
字段。索引。未分析);
//,Field.Store.NO,Field.Index.NOT_分析
iwriter.addDocument(文档);
}
iwriter.close();
//现在搜索索引:
logger.info(“HelloLucene.main:query2------”;
DirectoryReader ireader2=DirectoryReader.open(目录);
IndexSearcher isearcher2=新的IndexSearcher(ireader2);
logger.info(“HelloLucene.main:query2------”;
RegexQuery query2=新的RegexQuery(新术语(“名称”,“O.*));
query2.setRegexImplementation(新的JavaUtilRegexCapabilities(
标志(不区分大小写);
ScoreDoc[]hits2=isearcher2.search(query2,null,1000);
对于(int i=0;i
默认情况下,Lucene就是这样工作的。如果将字段标记为术语,则搜索字段中任何位置出现的术语。对于大型文本文档,这是绝对有意义的,因为您可能永远不希望只从大型文本体的开头进行搜索


如果您希望能够以文本字符串而不是标记化的术语集进行搜索,那么最好的解决方案是以一种能够很好地支持这一点的方式对其进行索引。A是一种典型的类型选择,而不是。

建议使用不带标记的字段。

另外,也不要使用通配符搜索,而是使用将生成标记的,并且将比通配符搜索快得多,因为它将在索引时发生

我已经知道了。我想搜索第一个字母是“A”的句子。它应该找到“Alice Run”而不是“Run Alice”。您可以设置为使用regex,例如您所指的regexQueryRhits(^a.*$”,newJavaUtilRegexCapabilities()<代码>RegexQueryRhits几乎不是标准的lucene库内容。。。无论如何,据我所知,Lucene RegexpQuery不支持
^
$
或任何其他类型的行开始/结束语法。您可以查看我的后期更新,然后运行示例吗?