Reprint Please specify source: http://blog.csdn.net/xiaojimanman/article/details/42884921
The index data in this case comes from the index created by the previous blog, which contains two documents, each of which has two domain name, content.
Index Search Demo
It's still the same. Look at a simple index search demo program before introduction.
/** * @Description: Index Search Demo */package com.lulei.lucene.study; Import Java.io.file;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.queryparser.classic.queryparser;import Org.apache.lucene.search.indexsearcher;import Org.apache.lucene.search.query;import Org.apache.lucene.search.topdocs;import Org.apache.lucene.store.directory;import Org.apache.lucene.store.fsdirectory;import org.apache.lucene.util.Version; public class Searchindex {public static void main (string[] args) {Directory directory = null;try {//index hard disk storage path Directory = FS Directory.open (New File ("D://study/index/testindex"));//Read index Directoryreader dreader = Directoryreader.open ( directory);//CREATE index Retrieve object Indexsearcher searcher = new Indexsearcher (Dreader); Specifies the word segmentation technology, the language processing module used here is consistent with the creation of the index, otherwise the results of the search is not ideal analyzer Analyzer = new StandardAnalyzer (version.lucene_43); Create query queries with the search term "space vector" Queryparser parse = new Queryparser (version.lucene_43, "content", analyzer); Query query = parse.parse ("Space vector");//Retrieves the index, gets the first 10 records that meet the criteria Topdocs Topdocs = searcher.search (query), if (topdocs! = null) { System.out.println ("Find a total of" + Topdocs.totalhits + "bar Qualifying Records");//loop Output record contents for (int i = 0; i < topDocs.scoreDocs.length; i++) {Document doc = Searcher.doc (topdocs.scoredocs[i].doc); System.out.println ("No." + (i + 1) + "bar content is--\tname:\t" + doc.get ("name") + "\tcontent:\t" + doc.get ("content");}} Close Resource Dreader.close ();d irectory.close ();} catch (Exception e) {e.printstacktrace ();}}}
The demo program runs the following results:
You can see that the keyword "space vector" search results for the second document in the index, you can get the corresponding domain value through the Document.get method.
Search Index Core class
In the above index search process, several core classes were used:Directory,directoryreader,indexsearcher,Analyzer, Query,topdocs, the previous introduction of the core class here will not continue to introduce, here is a description of the class that has not been introduced before.
Directoryreader
The Directoryreader is used to read the index, and the Dreader object created is used to create Indexsearcher objects for searching.
Indexsearcher
Indexsearcher is used for index search, this class exposes several search methods, it is the link index of the central link, you can see the Indexsearcher class as a read-only open index class, about indexsearcher deeper content, Will be described later in the blog.
Query
Lucene contains a number of specific query (query) subclasses, the query sub-class is: Termquery, Booleanquery, Phrasequery, Prefixquery, Phraseprefixquery, Termrangquery, Numericrangequery, Filteredquery, Spanquery, and so on, these subclasses will also be introduced in a future blog.
Topdocs
The Topdocs class is a simple pointer container in which the pointer generally refers to the search results of a forward n rank, and the search result is a document that matches the query criteria.
So far, we can use Lucene to achieve a simple index creation, search cases, in the following several blogs, I will introduce one by two, the word segmentation technology, query a lot of subclasses, Indexsearcher Search API, and so on, these parts can be read through the source code to understand.
Lucene-based Case development: Search index