Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize lucene4.7 Collector Collector

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to customize the lucene4.7 collector Collector". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Let's first review how a basic search process is completed.

1, get an index directory Directory (possibly memory-based or disk-based).

2, get a DirectoryReader.

3. Instantiate query component IndexSearcher.

4. Retrieve the TopDoc query result set

5. Traverse the results of ScoresDocs processing

If we take a look at this retrieval process, we can probably divide it into five steps. The first one is the preparation work, and the last two steps are the places where we often need to process the data. So which step do we Collector work in? In fact, Collector really works in 3-4 steps.

So what is the role of Collector? Why do I need Collector?

Before that, let's analyze the TopDocs class. In fact, the working principle of this class is to use a collector in the background to collect the results of our search, and to collect the hit data we retrieve through the two subclass collectors under the base class TopDocsCollector.

So the role of collector is to collect some result sets that we need to customize. In some cases, using collector can greatly improve the performance of our program. Through collector, we can do some unique customization operations on each matching document, of course, only if we need to use it.

Let's take a look at several methods of the collector base class

Method description collect () retrieval, each matching the last document, this method acceptsDocsOutOfOrder () will be called to test whether the collector can handle the unordered docidsetScorer (Scorer scorer) to handle the score of the retrieval results setNextReader (AtomicReaderContext context) retrieval, the method of switching between multiple index segment structures

Let's take a look at a custom collector to implement the function of the ScoreDoc class, the code is as follows.

Package com.piaoxuexianjing;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.lucene.index.AtomicReaderContext;import org.apache.lucene.search.Collector;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.Scorer;/** * @ author * @ version 1.0 * * Custom Collector * implement score Collection * * / public class MyScoreCollector extends Collector {/ / private HashMap documents=new HashMap () List docs=new ArrayList (); private Scorer scorer;//scorer class private int docBase;// global relative segment cardinality @ Override public boolean acceptsDocsOutOfOrder () {/ / TODO Auto-generated method stub / / return true is allowed unordered ID / / return false must be ordered return true } @ Override public void collect (int arg0) throws IOException {/ * matches the previous document * records its docid and scoring * / docs.add (new ScoreDoc (arg0+docBase,scorer.score (); / / built-in storage / / BinaryDocValues bookNames of the BinaryDocValues names;// character type / / built-in storage for BinaryDocValues ids;// character types / / built-in storage for BinaryDocValues prices;// character types / / FieldCache.Doubles d; / / built-in storage for numeric types / / built-in storage for FieldCache.Ints ints;// numeric types @ Override public void setNextReader (AtomicReaderContext arg0) throws IOException {this.docBase=arg0.docBase / / record the relative position of each index segment structure} @ Override public void setScorer (Scorer arg0) throws IOException {/ / TODO Auto-generated method stub this.scorer=arg0;// record matching score}}

Test the core code of the class

/ / Custom collector MyScoreCollector scoreCollector=new MyScoreCollector (); searcher.search (new MatchAllDocsQuery (), scoreCollector); / * * Custom collection class, implementation effect = = > ScoreDocs class * * / List slicscoreCollector.docs; for (ScoreDoc sc:s) {System.out.println (sc.doc+ "= =" + sc.score) }

The output is as follows

This is the end of the introduction of "how to customize the lucene4.7 Collector Collector". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report