使用 Lucene 统计分类结果
我正在尝试使用 Lucene Java 2.3.2 来实现对产品目录的搜索.除了产品的常规字段外,还有一个名为类别"的字段.一个产品可以属于多个类别.目前,我使用 FilteredQuery 在每个类别中搜索相同的搜索词,以获取每个类别的结果数.
I am trying to use Lucene Java 2.3.2 to implement search on a catalog of products. Apart from the regular fields for a product, there is field called 'Category'. A product can fall in multiple categories. Currently, I use FilteredQuery to search for the same search term with every Category to get the number of results per category.
这会导致每个查询进行 20-30 次内部搜索调用以显示结果.这大大减慢了搜索速度.有没有更快的方法使用 Lucene 实现相同的结果?
This results in 20-30 internal search calls per query to display the results. This is slowing down the search considerably. Is there a faster way of achieving the same result using Lucene?
推荐答案
这就是我所做的,虽然它有点占用内存:
Here's what I did, though it's a bit heavy on memory:
你需要的是提前创建一堆 BitSet
s,每个类别一个,包含一个类别中所有文档的doc id.现在,在搜索时,您使用 HitCollector 并对照 BitSet 检查文档 ID.
What you need is to create in advance a bunch of BitSet
s, one for each category, containing the doc id of all the documents in a category. Now, on search time you use a HitCollector and check the doc ids against the BitSets.
以下是创建位集的代码:
Here's the code to create the bit sets:
public BitSet[] getBitSets(IndexSearcher indexSearcher,
Category[] categories) {
BitSet[] bitSets = new BitSet[categories.length];
for(int i=0; i<categories.length; i++)
{
Query query = categories[i].getQuery();
final BitSet bitset = new BitSet()
indexSearcher.search(query, new HitCollector() {
public void collect(int doc, float score) {
bitSet.set(doc);
}
});
bitSets[i] = bitSet;
}
return bitSets;
}
这只是一种方法.您可能会使用 TermDocs 如果您的类别足够简单,则不要运行完整搜索,但无论如何,这应该只在您加载索引时运行一次.
This is just one way to do this. You could probably use TermDocs instead of running a full search if your categories are simple enough, but this should only run once when you load the index anyway.
现在,当需要计算搜索结果的类别时,您可以这样做:
Now, when it's time to count categories of search results you do this:
public int[] getCategroryCount(IndexSearcher indexSearcher,
Query query,
final BitSet[] bitSets) {
final int[] count = new int[bitSets.length];
indexSearcher.search(query, new HitCollector() {
public void collect(int doc, float score) {
for(int i=0; i<bitSets.length; i++) {
if(bitSets[i].get(doc)) count[i]++;
}
}
});
return count;
}
最终得到的是一个数组,其中包含搜索结果中每个类别的计数.如果您还需要搜索结果,则应将 TopDocCollector 添加到您的命中收集器(yo dawg ...).或者,您可以再次运行搜索.2 次搜索优于 30 次.
What you end up with is an array containing the count of every category within the search results. If you also need the search results, you should add a TopDocCollector to your hit collector (yo dawg...). Or, you could just run the search again. 2 searches are better than 30.
相关文章