Case development based on Lucene: A retrieval method in Indexsearcher

Source: Internet
Author: User
Reprint please indicate the source: http://blog.csdn.net/xiaojimanman/article/details/43052829
Before we introduced analyzer and query, we started the last class of the series Indexsearcher Search API introduction, Lucene key API not only here, there are IndexWriter, Field, Highlighter and so on, these are not introduced in this part, if the case used, and then do a brief introduction.
To view the API for Indexsearcher in Lucene4.3.1, click here for the search method as shown below:

From the above figure several methods can be seen, there are several key categories need to introduce: Query (previous blog introduced), Filter, Sort, Scoredoc, Collector

Collector
Collector is mainly used to collect the search results, custom sorting, filtering, and so on, in the Lucene4.3.1 API, there are two search methods used in collector, but there is a Lower-level search API below (low-level other search API) , if there is no need to use, as far as possible or using other methods.

Filter
Filter is mainly done by screening conditions, used to specify which documents can be in search results, this is not too much of their own use, query some information, introduced the filter is the search process is the data source to do the filter preprocessing (specified in filter), and then the results of the filter to the query statement, If so, the cost of using filter will be great, and his query may take several times more time. I don't think it's necessary to use filter, and if you really need to filter the results, you can combine those filters into query without having to create a filter object.

Sort
Sort specifies the sort method in the retrieval method, which is equivalent to an order by in the database, created like sort sort = new sort = new SortField ("Time", Type.long, True), The three parameters in the SortField constructor here represent the domain name, the field data type, and the Sort method (True descending/false ascending), where the example is sorted just by one field, if multiple fields can be added directly to the constructor, such as sort = new sort (new SortField ("Time", Type.long, True), New SortField ("Star", Type.int, False)).

Scoredoc
From the above method, the Searchafter method used in the Scoredoc, the method is mainly used in paging query, of course, can also use the search method, but there is a situation is irreplaceable, such as the first page 10 data, but because of promotion or advertising and other needs, You need to add a few (unknown) other records, but the front end can only display 10 pieces of data, so that the last few data on the page can not be displayed, but on the next page you want to display these data, so using the Seach method implementation is a bit difficult, the case requirements Graphic Description:

Searchafter when you implement the above requirements, when you take down a page of data, just tell it the last scoredoc of the last query, and it can start querying the next page of data directly from that piece of data.

The class that is involved in the retrieval method is also the end of the introduction, now assemble these to the simplest search method for example search (query query, int n), which implements the search for the top n section of the conditional Query, where the sort uses the default correlation sort To add other objects to the method, complete their corresponding functionality, such as search (query query, int n, sort sort), which specifies how they are sorted. This blog mainly introduces Indexsearcher's search-related APIs, so no more test demos are written here.

IndexWriter
The first part of the original plan should be concluded now, and no other content to do the relevant introduction, but the reader's feedback said that the index of the addition and deletion of the method is not too clear, then here is a simple question of the following methods, their interest can be implemented first, add a new document to the index in the creation of the index this blog has been mentioned, This is no longer introduced here.
modifying indexes
	public boolean updatedocument (Term Term, Document doc) {
		try {
			indexwriter.updatedocument (Term, doc);
			return true;
		} catch (IOException e) {
			e.printstacktrace ();
			return false;
		}
	}
The term here Specifies the index document to be modified, typically using the unique identification of the document in the index.
Delete Index
	public boolean deletedocument (query query) {
		try {
			indexwriter.deletedocuments (query);
			return true;
		} catch (IOException e) {
			e.printstacktrace ();
			return false;
		}
	}
Query here specifies the conditions that the document needs to meet, and there are ways to clear the index directly
	public boolean DeleteAll () {
		try {
			indexwriter.deleteall ();
			return true;
		} catch (IOException e) {
			e.printstacktrace ();
			return false;
		}
	}
All of these operations need to be performed indexwriter.commit () before they are saved, otherwise they will not be valid.


Note: The first part: Lucene's basic principles and the use of API simple interface to this end, according to the next part of the data flow should introduce data collection, but in order to introduce the background part of the search does not forget the relevant knowledge of Lucene, here the search background part of a little earlier. Before I start to search the background section, I will also in 1-2 blog, introduce the case of the demo and the background of the system architecture, in the overall requirements have a certain understanding of the foundation, we start case development. If the individual's blog in the layout or content and other aspects of the relevant views, sincerely hope that in the comments can be mentioned, I will actively adopt your suggestions, this series of blogs do a good job, we common progress.


PS: Recently found that other sites may be reproduced on the blog, there is no source link, such as to see more on the case development based on Lucene click here. Or visit the URL http://blog.csdn.net/xiaojimanman/article/category/2841877

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.