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 use the lucene4.7 sorting method

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to use lucene4.7 sorting method". In daily operation, I believe many people have doubts about how to use lucene4.7 sorting method. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use lucene4.7 sorting method". Next, please follow the editor to study!

Before that, let's familiarize ourselves with the basic knowledge of sorting in lucene. By default, Lucene uses a descending order of relevance as the default sorting method, which makes our search results usually optimal, because it will make the first few results most relevant to what we search for as far as possible, without requiring us to turn the pages to find what we want most. Compared with the database, this is a great advantage of full-text retrieval. Of course, in the actual development, we also need to provide a variety of different sorting methods for our customers according to the actual situation of the business. Let's first take a look at two basic sorting methods that are special in Lucene.

Attribute meaning in attribute SortField in Sort Sort.INDEXORDERSortField.FIELD_DOC is sorted in the order of the index Sort.RELEVANCESortField.FIELD_SCORE is sorted by relevance score

Let's take a look at a few more methods for retrieval.

= SortField class = / / field is sorted field type is sorted type public SortField (String field, Type type); / / field is sorted field type is sorted type reverse specified ascending or descending / / reverse is true descending false is ascending public SortField (String field, Type type, boolean reverse) = Sort class = public Sort (); / / Sort object constructor defaults to sorting public Sort (SortField field) by document score; / / A SortField public Sort (SortField...) sorted by document score Fields) / / multiple SortField sorted can be passed in an array = IndexSearche class r=//query is the Query object of the query filter is the number returned by filter n sort is sorted search (Query query, Filter filter, int n, Sort sort) / / doDocScores is true each hit result is scored / search (Query query, Filter filter, int n, Sort sort, boolean doDocScores, boolean doMaxScore) is scored in the case where doMaxScore is true

1. Before we sort a little bit, let's take a look at the contents of the index. The core code is as follows:

TopDocs topDocs=searcher.search (new MatchAllDocsQuery (), 10000)

2. After using the default relevance score, the core code and running effect are as follows:

Sort sort=new Sort (); / / default uses the relevance score TopDocs topDocs=searcher.search (new MatchAllDocsQuery (), 10000 pencil sort).

The reason for the garbled characters in the image above is that lucene will not grade the search results by default, because the scoring operation will degrade performance, so the column about score returns the string of NAN. For format reasons, when Sanxian uses the DecimalFormat class to retain 2 decimal places for its scoring results, the above picture occurs because it is a special character.

3, sort by date descending order, the core code and the running effect are as follows:

Sort sort=new Sort (new SortField ("date", Type.INT,true)); / / true is to sort TopDocs topDocs=searcher.search in descending order (new MatchAllDocsQuery (), 10000 sort)

4, sort by price ascending order, the core code and the running effect are as follows:

Sort sort=new Sort (new SortField ("price", Type.DOUBLE,false)); / / false is to sort TopDocs topDocs=searcher.search in descending order (new MatchAllDocsQuery (), 10000 sort)

5. In the case of multi-field sorting, in the case of descending order by date, since the dates with id of 7 and 8 are the same, we add a sorting field in ascending order of ename. The core code and the running effect are as follows:

/ Sort sort=new Sort (new SortField ("date", Type.INT, true), new SortField ("ename", Type.STRING, false); / / the effect of the two codes is the same Sort sort=new Sort (new SortField [] {new SortField ("date", Type.INT, true), new SortField ("ename", Type.STRING, false)}); TopDocs topDocs=searcher.search (new MatchAllDocsQuery (), 10000 sort)

6. Sort with score. Note that the last two Boolean variables can control whether to score or not. It is recommended not to turn it on when there is no requirement for scoring. A large number of variables have a great impact on performance. The results obtained by "programming" are sorted by default in descending order of rating. The core code and the running effect diagram are as follows:

Sort sort=Sort.RELEVANCE; TopDocs topDocs=searcher.search (new TermQuery (new Term ("bookname", "programming"), null,100,sort,true,true)

In the above programming, because the tf programmed in syncopation appears twice, it has a higher score in the query, so it ranks first.

7, pay attention to a few points

(1) sorting does not store any fields in a document, and string sorting will be in the first place.

(2) sorting does not store any fields in a document. Sorting with a numeric type will give it a default value of 0 to sort.

(3) We can code control the document with null value of numeric type, and we can set it to the maximum, so it will be at the bottom, the code is as follows

SortField sortField = new SortField ("value", SortField.Type.INT); sortField.setMissingValue (Integer.MAX_VALUE); at this point, the study on "how to use the lucene4.7 sorting method" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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