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作为额外的过滤器,这确实看起来很浪费.除非你有一些独特的搜索要求,否则这是我能想到的最有效的搜索方式.