【问题标题】:With Lucene 4.3.1, How to get all terms which occur in sub-range of all docs使用 Lucene 4.3.1,如何获取出现在所有文档子范围内的所有术语
【发布时间】:2023-04-09 16:38:02
【问题描述】:

假设一个具有以下字段的 lucene 索引:日期、内容。 我想获取日期为昨天的文档的所有术语值和频率。日期字段是关键字字段。内容字段被分析和索引。

请帮助我提供示例代码。

【问题讨论】:

  • 显示你已经尝试过的代码?
  • 我可以通过 ... Fields fields = MultiFields.getFields(searcher.getIndexReader());条款 terms = fields.terms("content"); termsEnum eachTerm = terms.iterator(null);
  • 我的临时解决方案是1.获取具有一定日期范围的docid,2.分析每个文档并通过程序制作词频3.对频率进行排序4.获取top-n词。是否只有 lucene api 有其他解决方案?请告诉我!

标签: lucene


【解决方案1】:

我的解决方案来源如下...

/**
 * 
 * 
 * @param reader
 * @param fromDateTime
 *            - yyyymmddhhmmss 
 * @param toDateTime
 *            - yyyymmddhhmmss 
 * @return 
 */
static public String top10(IndexSearcher searcher, String fromDateTime,
        String toDateTime) {
    String top10Query = "";
    try {
        Query query = new TermRangeQuery("tweetDate", new BytesRef(
                fromDateTime), new BytesRef(toDateTime), true, false);
        final BitSet bits = new BitSet(searcher.getIndexReader().maxDoc());
        searcher.search(query, new Collector() {

            private int docBase;

            @Override
            public void setScorer(Scorer scorer) throws IOException {
            }

            @Override
            public void setNextReader(AtomicReaderContext context)
                    throws IOException {
                this.docBase = context.docBase;
            }

            @Override
            public void collect(int doc) throws IOException {
                bits.set(doc + docBase);
            }

            @Override
            public boolean acceptsDocsOutOfOrder() {
                return false;
            }
        });

        //
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43,
                EnglishStopWords.getEnglishStopWords());

        //
        HashMap<String, Long> wordFrequency = new HashMap<>();
        for (int wx = 0; wx < bits.length(); ++wx) {
            if (bits.get(wx)) {
                Document wd = searcher.doc(wx);
                //
                TokenStream tokenStream = analyzer.tokenStream("temp",
                        new StringReader(wd.get("content")));
                // OffsetAttribute offsetAttribute = tokenStream
                // .addAttribute(OffsetAttribute.class);
                CharTermAttribute charTermAttribute = tokenStream
                        .addAttribute(CharTermAttribute.class);
                tokenStream.reset();
                while (tokenStream.incrementToken()) {
                    // int startOffset = offsetAttribute.startOffset();
                    // int endOffset = offsetAttribute.endOffset();
                    String term = charTermAttribute.toString();
                    if (term.length() < 2)
                        continue;
                    Long wl;
                    if ((wl = wordFrequency.get(term)) == null)
                        wordFrequency.put(term, 1L);
                    else {
                        wl += 1;
                        wordFrequency.put(term, wl);
                    }
                }
                tokenStream.end();
                tokenStream.close();
            }
        }
        analyzer.close();

        // sort
        List<String> occurterm = new ArrayList<String>();
        for (String ws : wordFrequency.keySet()) {
            occurterm.add(String.format("%06d\t%s", wordFrequency.get(ws),
                    ws));
        }
        Collections.sort(occurterm, Collections.reverseOrder());

        // make query string by top 10 words
        int topCount = 10;
        for (String ws : occurterm) {
            if (topCount-- == 0)
                break;
            String[] tks = ws.split("\\t");
            top10Query += tks[1] + " ";
        }
        top10Query.trim();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
    // return top10 word string
    return top10Query;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2012-02-14
    相关资源
    最近更新 更多