• <small id='y2u6I'></small><noframes id='y2u6I'>

        <bdo id='y2u6I'></bdo><ul id='y2u6I'></ul>
    1. <legend id='y2u6I'><style id='y2u6I'><dir id='y2u6I'><q id='y2u6I'></q></dir></style></legend>
    2. <i id='y2u6I'><tr id='y2u6I'><dt id='y2u6I'><q id='y2u6I'><span id='y2u6I'><b id='y2u6I'><form id='y2u6I'><ins id='y2u6I'></ins><ul id='y2u6I'></ul><sub id='y2u6I'></sub></form><legend id='y2u6I'></legend><bdo id='y2u6I'><pre id='y2u6I'><center id='y2u6I'></center></pre></bdo></b><th id='y2u6I'></th></span></q></dt></tr></i><div id='y2u6I'><tfoot id='y2u6I'></tfoot><dl id='y2u6I'><fieldset id='y2u6I'></fieldset></dl></div>

        <tfoot id='y2u6I'></tfoot>

        使用 Lucene 统计分类结果

        时间:2023-09-29

      1. <small id='ItzHn'></small><noframes id='ItzHn'>

          <tbody id='ItzHn'></tbody>

          • <tfoot id='ItzHn'></tfoot>
          • <i id='ItzHn'><tr id='ItzHn'><dt id='ItzHn'><q id='ItzHn'><span id='ItzHn'><b id='ItzHn'><form id='ItzHn'><ins id='ItzHn'></ins><ul id='ItzHn'></ul><sub id='ItzHn'></sub></form><legend id='ItzHn'></legend><bdo id='ItzHn'><pre id='ItzHn'><center id='ItzHn'></center></pre></bdo></b><th id='ItzHn'></th></span></q></dt></tr></i><div id='ItzHn'><tfoot id='ItzHn'></tfoot><dl id='ItzHn'><fieldset id='ItzHn'></fieldset></dl></div>

              • <bdo id='ItzHn'></bdo><ul id='ItzHn'></ul>
                <legend id='ItzHn'><style id='ItzHn'><dir id='ItzHn'><q id='ItzHn'></q></dir></style></legend>

                  本文介绍了使用 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:

                  你需要的是提前创建一堆 BitSets,每个类别一个,包含一个类别中所有文档的doc id.现在,在搜索时,您使用 HitCollector 并对照 BitSet 检查文档 ID.

                  What you need is to create in advance a bunch of BitSets, 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.

                  这篇关于使用 Lucene 统计分类结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:你如何在 .net 上运行 Lucene? 下一篇:如何在 Lucene 3.0.2 中索引和搜索文本文件?

                  相关文章

                  最新文章

                  <legend id='K8PTs'><style id='K8PTs'><dir id='K8PTs'><q id='K8PTs'></q></dir></style></legend>
                • <tfoot id='K8PTs'></tfoot>

                  <small id='K8PTs'></small><noframes id='K8PTs'>

                  <i id='K8PTs'><tr id='K8PTs'><dt id='K8PTs'><q id='K8PTs'><span id='K8PTs'><b id='K8PTs'><form id='K8PTs'><ins id='K8PTs'></ins><ul id='K8PTs'></ul><sub id='K8PTs'></sub></form><legend id='K8PTs'></legend><bdo id='K8PTs'><pre id='K8PTs'><center id='K8PTs'></center></pre></bdo></b><th id='K8PTs'></th></span></q></dt></tr></i><div id='K8PTs'><tfoot id='K8PTs'></tfoot><dl id='K8PTs'><fieldset id='K8PTs'></fieldset></dl></div>

                    • <bdo id='K8PTs'></bdo><ul id='K8PTs'></ul>