lucene4.1 小例子 全文检索

自己需要到一个全文检索,就搜了一下整理了一个简单可用的代码 实现很简单

首先需要 lucene 的包 :  lucene包下载  需要其中的 lucene-analyzers-common-4.1.0.jar,lucene-core-4.1.0.jar,lucene-queries-4.1.0.jar,lucene-queryparser-4.1.0.jar,paoding-analysis.jar 

然后就是代码。

先说一下 目的:


我的数据库里面一个字段,存的 内容 我想检索出来 含有我搜索条件的内容:

例如   产品类 product   里面有ID  和内容 

我想搜 内容含有 我们是朋友的  这些产品


首先 先实现 这个 produce类 当然 你可以按照你的需求来写

package com.mei.testlucene;

public class Product {
	
	private String id;
	
	private String content;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	

}

然后,


就用lucene 实现 搜索:


package com.mei.testlucene;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;

public class HelloLucene {
	
	
	public String helloL(String indexPath,List<Product> productList) throws IOException, ParseException{
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
		 // Store the index in memory:
	    Directory directory = new RAMDirectory();
	    // To store an index on disk, use this instead:
	    //Directory directory = FSDirectory.open(new File("H:/luceneIndex"));
	    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
	    IndexWriter iwriter = new IndexWriter(directory, config);
		for(Product product:productList){
            Document doc = new Document();
            doc.add(new Field("id",product.getId()+"",TextField.TYPE_STORED));
            doc.add(new Field("content",product.getContent()+"",TextField.TYPE_STORED));
            iwriter.addDocument(doc);
        }
 		iwriter.close();
	    System.out.println("建立索引 ok");
	    
	    
	    DirectoryReader ireader = DirectoryReader.open(directory);
	    IndexSearcher isearcher = new IndexSearcher(ireader);
	    // Parse a simple query that searches for "text":
	    QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "content", analyzer);
	    Query query = parser.parse("我们是朋友");
	    ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
	    System.out.println(hits.length == 1);
	    // Iterate through the results:
	    for (int i = 0; i < hits.length; i++) {
	      Document hitDoc = isearcher.doc(hits[i].doc);
	      System.out.println(hitDoc.get("content"));
	      System.out.println(hitDoc.get("id"));

	    }
	    ireader.close();
	    directory.close();
	    
		return "";
	}
	
	
  public static void main(String[] args) throws IOException, ParseException {
	  HelloLucene h = new HelloLucene();
	  List<Product> plist = new ArrayList<Product>();

	  Product p = new Product();
	  p.setId("1");
	  p.setContent(" 我是朋友我你说呢哇纷纷反而反而个");
	  Product p1 = new Product();
	  p1.setId("2");
	  p1.setContent("中华人民我们是");
	  Product p2 = new Product();
	  p2.setId("3");
	  p2.setContent("圣地阿森纳屌丝男士");
	  Product p3 = new Product();
	  p3.setId("4");
	  p3.setContent("收定金多少奶粉丰富让我给你温柔跟那人那个人 ");
	  
	  plist.add(p);
	  plist.add(p1);
	  plist.add(p2);
	  plist.add(p3);
	  
 	  h.helloL("", plist);
	  
		/*Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
		 // Store the index in memory:
	    Directory directory = new RAMDirectory();
	    // To store an index on disk, use this instead:
	    //Directory directory = FSDirectory.open(new File("H:/luceneIndex"));
	    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer);
	    IndexWriter iwriter = new IndexWriter(directory, config);
	     
	    Document doc = new Document();
	    String text = "This is the text to be indexed.";
	    doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
	    iwriter.addDocument(doc);
	    iwriter.close();
	    System.out.println("建立索引 ok");*/
	    
	    // Now search the index:
	    /*DirectoryReader ireader = DirectoryReader.open(directory);
	    IndexSearcher isearcher = new IndexSearcher(ireader);
	    // Parse a simple query that searches for "text":
	    QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer);
	    Query query = parser.parse("text");
	    ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
	    System.out.println(hits.length == 1);
	    // Iterate through the results:
	    for (int i = 0; i < hits.length; i++) {
	      Document hitDoc = isearcher.doc(hits[i].doc);
	      System.out.println(hitDoc.get("fieldname"));
	    }
	    ireader.close();
	    directory.close();*/
	}
}



运行结果就是:

建立索引 ok
false
 我是朋友我你说呢哇纷纷反而反而个
1
中华人民我们是
2
收定金多少奶粉丰富让我给你温柔跟那人那个人 
4


检索成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值