field of Lucene-lucene and its paging query

Source: Internet
Author: User
Tags create index

1 Use of Lucene field 1.1 field feature

Document is the carrier of field (field), and a document consists of multiple fields. field consists of a name and a value, and the value of field is the content to be indexed and the content to search for.

    • Whether participle (tokenized)

      is: The value of field is the word processing, the purpose of the word is to index . such as: Product name, product description. These content users will be queried by entering keywords, because of the variety of content, the need to do word processing indexing.

      No: Do not do word processing. such as: order number, ID number, is a whole, the word after the loss of meaning, it does not need participle.

    • Whether to index (indexed)

      is: the words (or the whole field content) are indexed and stored in the Index field after the field content is processed. The purpose of the index is to search. such as: commodity name, Commodity Description need to index word. The order number and the ID number are indexed as a whole. indexes are required whenever possible as a term for user query criteria.

      No: No index. such as: Product picture path, not as a query criteria, do not need to establish an index.

    • Whether to store (stored)

      Yes: Saves the field value to document. such as: commodity name, commodity price. any future content displayed to the user on the search results page will need to be stored.

      No: not stored. such as: Product description. The content is large in many formats and does not need to be displayed directly on the search results page, not stored. It can be taken from a relational database when needed.

1.2 Common field types

The following are the field types commonly used in enterprise project development:

field Type Data Type whether participle whether to index whether to store Description
Stringfield (FieldName, Fieldvalue, Store.yes) String N Y y/n String Type field, no participle, indexed as a whole (e.g., ID number, order number), whether it needs storage determined by Store.yes or store.no
Longfield (FieldName, Fieldvalue, Store.yes) Numeric representation Y Y y/n A long numeric field represents, participles, and indexes (such as: Price), whether the storage needs to be determined by store.yes or store.no
Storedfield (FieldName, Fieldvalue) Overloaded methods that support multiple types N N Y Build different types of field, no participle, no index, to store. (such as: Product picture path)
TextField (FieldName, Fieldvalue, store.no) Text type Y Y y/n Text Type field, Word breaker and index, whether storage needs to be determined by store.yes or store.no
1.3 Commonly used field types use 1.3.1 to prepare the environment

Copy the lucene-starter program and Java API simple use of the Lucene-first project, modify the name of Lucene-second;

Modify the Pom.xml file to modify all the Lucene-first to Lucene-second.

1.3.2 Demand Analysis
    • Book ID

      Whether participle: no participle required

      Index: Requires index (this can be indexed or not indexed)

      Storage: Storage Required

      -Stringfield

    • Book Name

      Whether participle: need participle

      Index: Required Index

      Storage: Storage Required

      -TextField

    • Book Prices

      Whether participle: need participle (lucene to numeric field, use inner part of word)

      Index: Required Index

      Storage: Storage Required

      -Floatfield

    • Book Pictures

      Whether participle: no participle required

      Index: No index required

      Storage: Storage Required

      -Storedfield

    • Book description

      Whether participle: need participle

      Index: Required Index

      Storage: No storage required

      -TextField

1.3.1 Modifying code
public class Indexmanager {/** * Test to create INDEX function * @throws Exception */@Test public void CreateIndex () th        Rows ioexception{//1. Collect data Bookdao Bookdao = new Bookdaoimpl ();                list<book> books = Bookdao.listall (); 2.        Create Document Object List<document> documents = new arraylist<document> ();            for (book book:books) {Document document = new document (); Add a domain//Add method to a Document object: Add a field to the Document object, field parameter: The domain to add//TextField: Text field, property name: field's names, Value: Field values, store: Specify Whether to save the domain value to the document//book ID--Stringfield document.add (New Stringfield ("BookId", Book.getid () + "", Sto Re.            YES));            Book name--TextField Document.add (New TextField ("BookName", Book.getbookname (), store.yes));            Book Price--Floatfield Document.add (New Floatfield ("Bookprice", Book.getprice (), store.yes)); Book Pictures--Storedfield Document.add (nEW Storedfield ("Bookpic", Book.getpic ()));            Book Description--TextField Document.add (New TextField ("Bookdesc", Book.getbookdesc (), store.no));        Adds a Document object to the collection of document objects Documents.Add (documents); }//3.                Create parser Object (Analyzer) for Word Analyzer Analyzer = new StandardAnalyzer (); 4. Create an index configuration object (indexwriterconfig) for configuring Lucene//Parameter one: The Lucene version currently in use, parameter two: parser indexwriterconfig indexconfig = new in                Dexwriterconfig (version.lucene_4_10_2, analyzer); 5.        Create an index library directory location Object (directory) that specifies the storage location of the index library file path = new file ("/users/healchow/documents/index");                Directory directory = fsdirectory.open (path); 6.                Create an index write Object (indexwriter), write the Document object to the index indexwriter indexwriter = new IndexWriter (directory, indexconfig); 7. Use the IndexWriter object to create an index for (document Doc:documents) {//Adddocement (DOC): Writes a Document object to the index library Indexwri        Ter.adddocument (DOC); }               8.    Release resources Indexwriter.close (); }}
1.3.2 re-establishing the index

Delete the previously established index and re-establish the index. Open the Luke tool to view the index information, you can see the book picture is not participle, so there is no index, the book price using Lucene internal participle, so according to UTF-8 decoding after the display garbled, as shown:

The description information of the book is not stored:
?

2 Lucene Paged Query 2.1 lucene page query

When you have too many searches, you need to consider pagination display, such as:

Description: The paged query for Lucene is implemented in memory.

2.2 Code Examples
/** * Retrieving indexes (implementing paging) * @throws Exception */@Testpublic void Searchindexbypage () throws Exception {//1. Creating a Parser Object (analyzer)    , for Word breaker//Analyzer Analyzer = new StandardAnalyzer ();        Use the IK Chinese word breaker analyzer Analyzer = new Ikanalyzer (); 2. Create query Object (query)//Bookname:lucene//2.1 Create Query Parser object//parameter one: Specify a default search domain//Parameter two: parser queryparser queryparser =    New Queryparser ("BookName", analyzer);        2.2 Using the query parser object, instantiate the query object//parameter: query expression, queries query = Queryparser.parse ("Bookname:lucene"); 3. Create index Library directory location object (directory), specify the location of the index Library Directory directory = Fsdirectory.open ("/users/healchow/documents/index")         ; 4.        Create an index read object (indexreader) to read the index Indexreader reader = directoryreader.open (directory); 5.        Create index Search object (indexsearcher) to perform search Indexsearcher searcher = new Indexsearcher (reader); 6. Use the Indexsearcher object, perform a search, return a search result set Topdocs//Search method: Perform search//Parameter one: Query object//Parameter two: N-Specifies the first n topdocs t of the search results after the order is returned Opdocs = Searcher. Search (query, 10); 7.    Processing result set//7.1 Print the number of results actually queried System.out.println ("Number of results actually queried:" +topdocs.totalhits);        7.2 Get the result array of the search//Scoredoc: There is the ID of the document we need, the score of the document we need scoredoc[] Scoredocs = Topdocs.scoredocs;    Add page Implementation ========= start//Current page int page = 2;    Size of each page int pageSize = 2;    Calculate record start number int start = (page-1) * pageSize; Calculate the number of record terminations--get the minimum value of the data index and the actual result number to prevent the array from overstepping//Data index: Start + pageSize//actual number of results: scoredocs.length int end = MATH.MI    N (start + pageSize, scoredocs.length);  Add paging implementation ========= end for (int i = start; i < end; i++) {System.out.println ("= = = = = = = = = = = = = = = =        = = = = =");        Get document ID and score int docId = Scoredocs[i].doc;        Float score =scoredocs[i].score;        SYSTEM.OUT.PRINTLN ("Document ID:" + docId + ", Document Rating:" + score);                Querying the document data according to the document ID-equivalent to querying documents in a relational database based on the primary key id doc = Searcher.doc (docId);        System.out.println ("Book ID:" + doc.get ("bookId")); SysTem.out.println ("Book Name:" + doc.get ("BookName"));        System.out.println ("Book Price:" + doc.get ("Bookprice"));        System.out.println ("Book Picture:" + doc.get ("Bookpic"));    System.out.println ("Book Description:" + doc.get ("Bookdesc")); }//8. Release resource Reader.close ();}
2.3 Paged Query Results

There are 3 results in this query, so there is only one result on page 2nd:

Copyright Notice

Author: Ma_shoufeng (Ma Ching)

Source: Blog Park Ma Ching's Blog

Your support is a great encouragement to bloggers, thank you for your reading.

The copyright of this article is owned by bloggers, welcome reprint, but without the blogger agreed to retain this paragraph statement, and in the article page obvious location to the original link, otherwise Bo Master reserves the right to pursue legal responsibility.

field of Lucene-lucene and its paging query

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.