Step by step with me. Lucene (7)---The indexsearcher construction process of Lucene search

Source: Internet
Author: User
Tags float max

Recently has been writing step by step with me to learn Lucene series (http://blog.csdn.net/wuyinggui10000/article/category/3173543), personal blog also received a lot of traffic, thank you for your attention, This is also a personal incentive for me, O (∩_∩) o haha ~, personal feeling in the process of writing a blog to harvest a lot, I will continue to work, in the process of their own will also write more similar series of blog, but also is a kind of accumulation; indexsearcher

Search engine construction is divided into index content and query index two big aspects, here is to introduce the Lucene index query is the construction process of indexsearcher;

First understand the following indexsearcher:

    • Indexsearcher provides a query implementation for a single indexreader;
    • We can query the index by invoking the search (query,n) or search (Query,filter,n) method;
    • In the case of small changes in index content, we can improve the performance by using a single indexsearcher sharing method for indexed searches;
    • If the index changes, we need to use directoryreader.openifchanged (Directoryreader) to get the new reader, and then create a new Indexsearcher object;
    • In order to make the query latency low, we'd better use near real-time search methods (at this point our Directoryreader build will be adopted DirectoryReader.open(IndexWriter, boolean) )
    • Indexsearcher instances are fully thread-safe, which means that multiple threads can call any method concurrently. If external synchronization is required, there is no need to add indexsearcher synchronization;

Indexsearcher the creation process
    • Creates an instance of Fsdirectory based on the index file path, and the returned Fsdirectory instance is related to the system or run environment, for Linux, MacOSX, Solaris, and Windows 64-bit JREs returns a Mmapdirectory instance, Niofsdirectory is returned for other non-Windows JREs environments, and simplefsdirectory for other Windows JRE environments. Its execution efficiency is reduced in turn
    • Directoryreader then reads the index file according to the Fsdirectory instance obtained and obtains the Directoryreader object The Directoryreader open method returns the principle of the instance: reading the contents of the segments file in the index directory, traversing the Segmentinfos in reverse and populating an array of segmentreader (an implementation of Indexreader), And build an example of Standarddirectoryreader
    • With Indexreader,indexsearcher object Instantiation personal overdraft, new Indexsearcher (Directoryreader) can get an instance of it. If we want to improve Indexsearcher's execution efficiency can be new Indexsearcher (Direcotoryreader,excuterservice) to create Indexsearcher objects, The benefit of this is to use a division of labor for each segment, but note that Indexsearcher does not maintain the excuterservice life cycle, and we also need to invoke Excuterservice's close/ Awaittermination
Related practices

The following is a search tool class written according to the Indexsearcher-related build process and its characteristics

Package Com.lucene.search;import Java.io.file;import Java.io.ioexception;import java.nio.file.paths;import Java.util.concurrent.executorservice;import Org.apache.lucene.analysis.analyzer;import Org.apache.lucene.analysis.standard.standardanalyzer;import Org.apache.lucene.document.document;import Org.apache.lucene.index.directoryreader;import Org.apache.lucene.index.indexreader;import Org.apache.lucene.index.multireader;import Org.apache.lucene.index.term;import Org.apache.lucene.search.booleanclause.occur;import Org.apache.lucene.search.booleanquery;import Org.apache.lucene.search.indexsearcher;import Org.apache.lucene.search.matchalldocsquery;import Org.apache.lucene.search.numericrangequery;import Org.apache.lucene.search.query;import Org.apache.lucene.search.scoredoc;import Org.apache.lucene.search.sort;import Org.apache.lucene.search.SortField ; Import Org.apache.lucene.search.sortfield.type;import Org.apache.lucene.search.termquery;import org.apache.lucene.search.topdocs;import org.Apache.lucene.search.topfieldcollector;import Org.apache.lucene.store.fsdirectory;import Com.lucene.index.indexutil;public class Searchutil {public static final Analyzer Analyzer = new StandardAnalyzer ();/** Get Indexsearcher object (used for single index directory query) * @param indexpath Index directory * @return * @throws ioexception * @throws interruptedexception */p Ublic static Indexsearcher getindexsearcher (String indexpath,executorservice Service,boolean Realtime) throws IOException, Interruptedexception{directoryreader reader = Directoryreader.open (Indexutil.getindexwriter (IndexPath , true), realtime); Indexsearcher searcher = new Indexsearcher (reader,service); if (service! = null) {Service.shutdown ();} return searcher;} /** Multi-threaded query * @param parentpath Parent Index directory * @param service multi-threaded query * @return * @throws IOException * @throws Interruptedexcepti On */public static Indexsearcher getmultisearcher (String parentpath,executorservice Service,boolean Realtime) throws IOException, Interruptedexception{multireader Multireader; File File = NEW File (Parentpath); file[] files = file.listfiles (); indexreader[] readers = new Indexreader[files.length];if (!realtime) {for (int i = 0; I &lt ; Files.length; i + +) {Readers[i] = Directoryreader.open (Fsdirectory.open (Paths.get (Files[i].getpath (), new string[0]));}} else{for (int i = 0; i < files.length; i + +) {Readers[i] = Directoryreader.open (Indexutil.getindexwriter (files[i].get Path (), True), true);}} Multireader = new Multireader (readers); Indexsearcher searcher = new Indexsearcher (multireader,service); if (Service! = NULL) {Service.shutdown ();} return searcher;} /** query from a specified configuration item * @return * @param analyzer Word breaker * @param field field * @param fieldtype field type * @param querystr query criteria * @param rang e whether interval query * @return */public static query Getquery (String field,string fieldtype,string Querystr,boolean range) {Query q = n Ull;if (querystr! = NULL &&! "". Equals (QUERYSTR)) {if (range) {string[] STRs = Querystr.split ("\\|"); if ("int". Equals (FieldType)) {int min = new integer (strs[0]); int max = new Integer (STRS[1]); q = numericrangequery.newintrange (field, Min, Max, True, true);} else if ("Double". Equals (FieldType)) {Double min = new double (strs[0]);D ouble max = new double (strs[1]); q = Numericrangeque Ry.newdoublerange (field, Min, Max, True, true);} else if ("float". Equals (FieldType)) {float min = new float (strs[0]); float max = new float (strs[1]); q = numericrangequery.newfloatrange (field, Min, Max, True, true);} else if ("Long". Equals (FieldType)) {Long min = new Long (strs[0]); Long max = new Long (strs[1]), q = Numericrangequery.newlongrange (field, Min, Max, True, True);}} Else{if ("int". Equals (FieldType)) {q = numericrangequery.newintrange (field, new Integer (QUERYSTR), new Integer ( QUERYSTR), True, true);} else if ("Double". Equals (FieldType)) {q = numericrangequery.newdoublerange (field, new double (QUERYSTR), new double ( QUERYSTR), True, true);} else if ("float". Equals (FieldType)) {q = numericrangequery.newfloatrange (field, new float (QUERYSTR), new float (querystr ), True, true);} Else{term term = new term (field, qUERYSTR); q = new termquery (term);}}} else{q= new Matchalldocsquery ();} SYSTEM.OUT.PRINTLN (q); return q;} /** multi-conditional query similar to SQL in * @param querys * @return */public static query Getmultiquerylikesqlin (query ... querys) {booleanquery qu ery = new Booleanquery (); for (Query Subquery:querys) {query.add (subquery,occur.should);} return query;} /** multi-conditional query similar to SQL and * @param querys * @return */public static query Getmultiquerylikesqland (query ... querys) {booleanquery query = new Booleanquery (); for (Query Subquery:querys) {query.add (subquery,occur.must);} return query;} /** to sort multiple criteria build sort criteria * @param fields * @param type * @param reverses * @return */public static sort Getsortinfo (string[] Fie Lds,type[] types,boolean[] reverses) {sortfield[] SortFields = null;int Fieldlength = Fields.length;int TypeLength = types . length;int reverlength = reverses.length;if (! ( Fieldlength = = typelength) | | ! (Fieldlength = = reverlength)) {return null;} Else{sortfields = new Sortfield[fields.length];for (int i = 0; i < fields.length; i++) {Sortfields[i] = new SortField (Fields[i], types[i], reverses[i]);}} return new Sort (SortFields);} /** query based on query, query criteria, per page, sorting criteria * @param query condition * @param first start value * @param max Max * @param sort sort Condition * @return */public S Tatic Topdocs Getscoredocsbyperpageandsortfield (indexsearcher searcher,query Query, int first,int max, sort sort) {try { if (query = = null) {SYSTEM.OUT.PRINTLN ("query is null return null"); return null;} Topfieldcollector collector = null;if (sort! = null) {collector = Topfieldcollector.create (sort, First+max, false, False, F Alse);} Else{sort = new Sort (new Sortfield[]{new SortField ("Modified", SortField.Type.LONG)}); collector = Topfieldcollector.create (sort, First+max, False, False, false);} Searcher.search (query, collector); return Collector.topdocs (First, max);} catch (IOException e) {//TODO auto-generated catch Block}return null;} /** gets the ID of the last index, the incremental update uses the * @return */public static Integer Getlastindexbeanid (Indexreader multireader) {query query = new Match Alldocsquery (); IndexSearcher Searcher = Null;searcher = new Indexsearcher (Multireader); SortField SortField = new SortField ("id", sortfield.type.int,true); Sort sort = new sort (new Sortfield[]{sortfield}); Topdocs docs = Getscoredocsbyperpageandsortfield (searcher,query, 0, 1, sort); scoredoc[] Scoredocs = docs.scoredocs;int Total = scoredocs.length;if (Total > 0) {scoredoc Scoredoc = scoredocs[0];D OCU ment doc = null;try {doc = Searcher.doc (scoredoc.doc);} catch (IOException e) {//TODO auto-generated catch blocke.printst Acktrace ();} return new Integer (Doc.get ("id"));} return 0;}}
The above is the Lucene search Indexsearcher construction process related content;

Related code download tomorrow will give the relevant link

Step by step with me to learn Lucene is a summary of the recent Lucene index, we have a question to contact my q-q: 891922381, at the same time I new Q-q group: 106570134 (Lucene,solr,netty,hadoop), such as Mongolia joined, Greatly appreciated, we discuss together, I strive for a daily Bo, I hope that we continue to pay attention, will bring you surprise


Step by step with me. Lucene (7)---The indexsearcher construction process of Lucene search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.