Lucence, lucene tutorial

Source: Internet
Author: User

Lucence, lucene tutorial

Lucene is a sub-project of the 4 jakarta Project Team of the apache Software Foundation. It is an open-source full-text retrieval engine toolkit, but it is not a complete full-text retrieval engine, it is a full-text search engine architecture that provides a complete query engine and index engine, some text analysis engines. Lucene aims to provide a simple and easy-to-use toolkit for software developers.

Paste this sentence to show that Lucene is only a toolkit, a search engine toolkit.

Someone will ask? What is the difference between Lucene and solr? solr is a search system. For example, the difference between servlet and struts2: Lucene is servlet. solr is like solr. solr encapsulates Lucene.

The following describes the principle of Lucene:

We use Lucene to actually use reverse query.

What is inverted query? For example

We have used the Xinhua Dictionary. The Xinhua Dictionary is divided into two parts: the first part is the edge of the directory, the second part is the text, one word explanation,

When we use the Xinhua Dictionary, we usually search for words through the sides of the blog. If no one has a page-by-page dictionary, find words.

Lucene's inverted ranking is like this. It will retrieve text, databases, and web pages, and divide the content into tokens, just like the radicals.

Emphasize again

The difference between a search engine (Baidu, Google) and lucene: a search engine is an application, while lucene is a search tool.

 

Name: lucene indicates that you want to search for a document in the Field "name" with the content "lucene. Desc: lucene AND desc: java indicates to search documents that include the keyword "lucene" AND "java. It doesn't matter if you don't understand it. Next I will explain the relationship between Doucment and Field. Here I will use a data description in the database.

This piece of data is a document

Each Field is a Field. next, let's talk about the lucene word divider, which is made by foreigners. If you don't talk about Chinese support, you also know that not many foreigners think about it, "I am a Chinese"> I am a Chinese man> this effect is not what we really want. What we want is a word like "China" and "Chinese people, I don't want to be confused here. There are a lot of Chinese Word Analyzer on the market. The Invincible existence is IK. This is a jar package. Just import the project, he is invincible because he can add words by himself, such as "diaosi" and "Gao fushuai". This word can also be added to the word divider by himself, so that the program can recognize it.

This is the package to be used;

After ik is downloaded, these three files must also be imported into the project. ext. dic is a wildcard and stop is a stopword.

 

All the above are Lucece theories. Only the theories can be understood, and the following code implementation process will be easy.

1 package com. itheima. lucene; 2 3 import java. io. file; 4 import java. util. arrayList; 5 import java. util. list; 6 7 import org. apache. lucene. analysis. analyzer; 8 import org. apache. lucene. analysis. standard. standardAnalyzer; 9 import org.apache.e.doc ument. document; 10 import org.apache.e.doc ument. field. store; 11 import org.apache.e.doc ument. textField; 12 import org. apache. lucene. index. directoryReader; 13 import org. apache. lucene. index. indexReader; 14 import org. apache. lucene. index. indexWriter; 15 import org. apache. lucene. index. indexWriterConfig; 16 import org. apache. lucene. queryparser. classic. queryParser; 17 import org. apache. lucene. search. indexSearcher; 18 import org. apache. lucene. search. query; 19 import org. apache. lucene. search. scoreDoc; 20 import org. apache. lucene. search. topDocs; 21 import org. apache. lucene. store. directory; 22 import org. apache. lucene. store. FSDirectory; 23 import org. apache. lucene. util. version; 24 import org. junit. test; 25 import org. wltea. analyzer. lucene. IKAnalyzer; 26 27 import com. itheima. dao. bookDao; 28 import com. itheima. dao. impl. bookDaoImpl; 29 import com. itheima. pojo. book; 30 31 public class CreateIndexTest {32 // word segmentation 33 @ Test 34 public void testCreateIndex () throws Exception {35 // 1. data collection 36 BookDao bookDao = new BookDaoImpl (); 37 List <Book> listBook = bookDao. queryBookList (); 38 39 // 2. create Document Object 40 List <Document> documents = new ArrayList <> (); 41 for (Book bk: listBook) {42 43 Document doc = new Document (); 44 doc. add (new TextField ("id", String. valueOf (bk. getId (), Store. YES); // Store. YES: 45 doc stored in the document domain. add (new TextField ("name", bk. getName (), Store. YES); 46 doc. add (new TextField ("price", String. valueOf (bk. getPrice (), Store. YES); 47 doc. add (new TextField ("pic", bk. getPic (), Store. YES); 48 doc. add (new TextField ("desc", bk. getDesc (), Store. YES); 49 50 // put the Document in the list 51 documents. add (doc); 52} 53 54 // 3. create Analyzer (word divider) 55 // analyzer Analyzer = new StandardAnalyzer (); 56 // Chinese IK 57 analyzer Analyzer = new IKAnalyzer (); 58 59 // 4. create IndexWriterConfig configuration information class 60 IndexWriterConfig config = new IndexWriterConfig (Version. LUCENE_4_10_3, analyzer); 61 62 // 5. create a Directory object and declare the index database storage location 63 Directory directory = FSDirectory. open (new File ("H: \ temp"); 64 65 // 6. create IndexWriter write object 66 IndexWriter writer = new IndexWriter (directory, config); 67 68 // 7. write the Document to the index database 69 for (Document doc: documents) {70 writer. addDocument (doc); 71} 72 73 // 8. release resource 74 writer. close (); 75} 76

// Query 77 @ Test 78 public void serachIndex () throws Exception {79 // The Analyzer created must be consistent with the analyzer retrieved. 80 Analyzer analyzer = new StandardAnalyzer (); 81 // create a search parser. The first parameter is the default Field, and the second parameter is the word divider 82 QueryParser queryParser = new QueryParser ("desc", analyzer ); 83 84 // 1. create Query search object 85 Query query = queryParser. parse ("desc: java AND lucene"); 86 87 // 2. create a Directory Stream object and declare the index database location 88 Directory directory = FSDirectory. open (new File ("H: \ temp"); 89 90 // 3. create an index read object IndexReader 91 IndexReader indexReader = DirectoryReader. open (directory); 92 93 // 4. create an index search object IndexSearcher 94 IndexSearcher indexSearcher = new IndexSearcher (indexReader); 95 96 // 5. use the index to search for objects and execute the search. The first parameter of the returned result set TopDocs 97 // is the search object. The second parameter is the number of returned data entries, 98 TopDocs topDocs = indexSearcher is returned for n data entries at the top of the query result. search (query, 10); 99 System. out. println ("the total number of queried data records is:" + topDocs. totalHits); 100 // obtain the result set 101 ScoreDoc [] docs = topDocs. scoreDocs; 102 103 // 6. resolution result set 104 for (ScoreDoc scoreDoc: docs) {105 // obtain the Document 106 int docID = scoreDoc.doc; 107 Document doc = indexSearcher.doc (docID); 108 109 System. out. println ("docID:" + docID); 110 System. out. println ("bookid:" + doc. get ("id"); 111 System. out. println ("pic:" + doc. get ("pic"); 112 System. out. println ("name:" + doc. get ("name"); 113 System. out. println ("desc:" + doc. get ("desc"); 114 System. out. println ("price:" + doc. get ("price"); 115} 116 117 // 7. release resource 118 indexReader. close (); 119} 120}

 

 

 

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.