java有in关键字吗_java – Lucene – 相当于SQL“IN”关键字

I would like to get Lucene text search results on the description but only from documents in the set of ids.

您需要使用BooleanQuery.

如果使用QueryParser创建查询,则使用:

+(id:6345 id:5759 id:333 ...) +(description:"blah*")

如果以编程方式创建Query,则代码将类似于:

BooleanQuery ids = new BooleanQuery();

ids.add(new TermQuery(new Term("id", "6345")), SHOULD);

ids.add(new TermQuery(new Term("id", "5759")), SHOULD);

ids.add(new TermQuery(new Term("id", "333")), SHOULD);

BooleanQuery resultQuery = new BooleanQuery();

resultQuery.add(new PrefixQuery(new Term("description", "blah")), MUST);

resultQuery.add(ids, MUST);

The set of ids maybe tens of thousands.

BooleanQuery已经为最大数量的子句构建了限制(请参阅org.apache.lucene.search.BooleanQuery#maxClauseCount).您需要使用BooleanQuery.setMaxClauseCount()增加此限制.这将要求您以编程方式创建查询.

Can I construct a Lucene query to handle this efficiently, or should I search my entire document index and then do a set intersection? Something else?

据我所知,倒排索引是最有效的搜索方式,目前人类已知.至少,从搜索时间的角度来看(不考虑索引阶段).

因此,如果关注效率,我建议将所有搜索逻辑移动到Lucene(它是反向索引库).作为一个非常成熟的图书馆,Lucene可以搜索几乎所有类型的信息.因此,可能所有文档都可以在Lucene中编制索引,所有“先前的查询”也可以在Lucene中执行.

在这种情况下,不需要向Lucene发送数千个ID作为额外的过滤器,这确实看起来很浪费.除非你有一些独特的搜索要求,否则这是我能想到的最有效的搜索方式.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!针对您的问题,可以使用 Lucene 的分词器和分析器来进行去除停用词和词干提取。以下是一个简单的示例: ```java import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.core.StopAnalyzer; import org.apache.lucene.analysis.en.PorterStemFilter; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.util.Version; import java.io.IOException; import java.io.StringReader; public class LuceneStemmingExample { public static void main(String[] args) throws IOException { String text = "Lucene is a Java full-text search engine. " + "Lucene is not a complete application, but rather a code library " + "and lucene is used to add search capability to applications."; Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_7_7_0); analyzer = new StopAnalyzer(Version.LUCENE_7_7_0); // 添加停用词 analyzer = new PorterStemFilter(analyzer); // 添加词干提取器 StringReader reader = new StringReader(text); TokenStream tokenStream = analyzer.tokenStream("", reader); CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class); tokenStream.reset(); while (tokenStream.incrementToken()) { System.out.println(charTermAttribute.toString()); } tokenStream.end(); tokenStream.close(); } } ``` 在上面的示例,我们使用了 Lucene 的 `StandardAnalyzer` 分析器来对文本进行分词,然后使用了 `StopAnalyzer` 停用词分析器来去除停用词,最后使用了 `PorterStemFilter` 词干提取器来对词进行词干提取。最终输出的结果如下: ``` lucene java full text search engine lucene complete application rather code library lucene used add search capability applications ``` 可以看到,输出的词已经被去除了停用词并被进行了词干提取。希望这个示例对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值