Lucene index database

Source: Internet
Author: User
Tags split words
This article clearly introduces how to transfer data from the database to the lucene index file. You can also refer to Che Dong's blog: www.chedong.comtechweb+e.html. Lucene, as an auxiliary tool for full-text search, provides conditional search for us, whether it is a search engine like Google or Baidu or a search function in the Forum.

This article clearly introduces how to transfer data from the database to the lucene index file. Also can refer to the car East blog: http://www.chedong.com/tech/weblucene.html this. Lucene, as an auxiliary tool for full-text search, provides conditional search for us, whether it is a search engine like Google or Baidu or a search function in the Forum.

This article clearly introduces how to transfer data from the database to the lucene index file. You can also refer to Che Dong's blog:
Http://www.chedong.com/tech/weblucene.html
This article.

Lucene, as an auxiliary tool for full-text search, provides conditional search for us, whether it is a search engine like Google or Baidu or a search function in the Forum, the search for other C/S architectures brings great convenience and high efficiency. This article mainly uses Lucene to index MS SQL Server 2000 and then perform full-text search. The database content can be webpage content or other content. In this article, the database content is an author table-Authors table in the library management system.

Due to the length of the article, this article will not be detailed, nor can it be further discussed.

This document uses the following structure:

1. Introduce the structure of the Authors table in the database

2. Create an index for the database

3. Create a query function for the database

4. query and display results on the web interface

1. Introduce the structure of the Authors table in the database

Field Name field type field meaning

Au_id Varchar (11) Author No.
Au_name Varchar (60) Author name
Phone Char (12) Phone number
Address Varchar (40) Address
City Varchar (20) City
State Char (2) Province
Zip Char (5) Zip code
Contract Bit (1) Foreign key (not relevant)

Some content in the table:

2. Create an index for the database

First, create a class TestLucene. java. This class is used to index databases and write query conditions.

Of course, the first step is to establish a database connection. The connection code is omitted here. Pai_^

Next, create a new method getResutl (String), which returns the content of the database table Authors. The Code is as follows:


Public ResultSet getResult (String SQL ){
Try {
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery (SQL );
Return rs;
}
Catch (SQLException e ){
System. out. println (e );
}
Return null;
}

Then, create an index for the database.

First, you need to define an IndexWriter (). It writes the index into Lucene's own database and stores it in your own location. In the definition of IndexWriter, you must specify its analyzer. Lucene has several analyzers, such as StandarAnalyzer (), SimpleAnalyzer (), and StopAnalyzer. It is used to analyze the text and determine how to split words.

Next, define a Document. Document is equivalent to the same row of data in the orders table. The Document contains the Field, which is equivalent to a column in the database, that is, an attribute and a Field. Finally, optimize IndexWriter. The method is very simple, that is, writer. optimize ().
The Code is as follows:
Public void Index (ResultSet rs ){
Try {
IndexWriter writer = new IndexWriter ("d:/index/", getAnalyzer (), true );
While (rs. next ()){
Document doc = new Document ();
Doc. add (Field. Keyword ("id", rs. getString ("au_id ")));
Doc. add (Field. Text ("name", rs. getString ("au_name ")));
Doc. add (Field. UnIndexed ("address", rs. getString ("address ")));
Doc. add (Field. UnIndexed ("phone", rs. getString ("phone ")));
Doc. add (Field. Text ("City", rs. getString ("city ")));
Writer. addDocument (doc );
}
Writer. optimize ();
Writer. close ();
}
Catch (IOException e ){
System. out. println (e );
}
Catch (SQLException e ){
System. out. println (e );
}
}

Public Analyzer getAnalyzer (){
Return new StandardAnalyzer ();
}

3. Create a query function for the database

Create a new searcher (String) method in TestLucene. It returns a structure set for search, which is the same as the ResultSet in the database. It represents the content you want to query. Here, I am writing the field to be queried to death. You can add a parameter to indicate the field to be queried.
There are two main objects: IndexSearcher and Query. IndexSearcher is used to find the index database, and Query is used to process the search. It contains three parameters: Query content, Query field, and analyzer.
The Code is as follows:


Public Hits seacher (String queryString ){
Hits hits = null ;;
Try {
IndexSearcher is = new IndexSearcher ("D:/index /");
Query query = QueryParser. parse (queryString, "City", getAnalyzer ());
Hits = is. search (query );
} Catch (Exception e ){
System. out. print (e );
}
Return hits;
}

4. query and display results on the web interface

Create a Jsp page TestLucene. jsp to search.

On the TestLucene. jsp page, first introduce the class
<% @ Page import = "etetest. LucentTest" %>
<% @ Page import = "org. apache. lucene. search. *, org.apache.e.doc ument. *" %>

Then define a deleetest object to obtain the query result set:
LucentTest lucent = new LucentTest ();
Hits hits = lucent. seacher (request. getParameter ("queryString "));

Define a Form and create a query environment:



Display query results:









Document doc=hits.doc (I );
%>







<% If (hits! = Null) {%> <% For (int I = 0; I <% }}%>
Author No. Author name Address Phone number
<% = Doc. get ("id") %> <% = Doc. get ("name") %> <% = Doc. get ("address") %> <% = Doc. get ("phone") %>

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.