Apache的lucene2.3.2来做索引

Apache的lucene2.3.2来做索引

1、导入jar包
在pom.xml中导入
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-memory</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>2.3.2</version>
</dependency>

2、POJO的基类
对象的基类BaseObject.java,基类中写id,gmtCreate,gmtModify等成员,避免每个类中重复书写
package com.sillycat.easyview.plugin.commons.base;
import java.io.Serializable;
public class BaseObject implements Serializable {
private static final long serialVersionUID = -5588271386305919216L;
private Integer id;
public Integer getId() {
   return id;
}
public void setId(Integer id) {
   this.id = id;
}
}

搜索类的接口,要实现build索引的接口LuceneObject.java :
package com.sillycat.easyview.plugin.commons.base;
import org.apache.lucene.document.Document;
public interface LuceneObject {
public Document buildindex();
}

真正的POJO例子User.java:
package com.sillycat.easyview.core.model;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import com.sillycat.easyview.plugin.commons.base.BaseObject;
import com.sillycat.easyview.plugin.commons.base.LuceneObject;
import com.sillycat.easyview.plugin.commons.utils.StringUtil;
public class User extends BaseObject implements LuceneObject {
private static final long serialVersionUID = -6050058005265846739L;
private String logonName;
private String email;
private String nickName;
private String phone;
private String mobile;
//建立索引
public Document buildindex() {
   Document doc = new Document();
   if (StringUtil.isNotBlank(this.getLogonName())) {
    doc.add(new Field("logonName", this.getLogonName(),
      Field.Store.YES, Field.Index.TOKENIZED));
   }
   if (StringUtil.isNotBlank(this.getEmail())) {
    doc.add(new Field("email", this.getEmail(), Field.Store.YES,
      Field.Index.TOKENIZED));
   }
   return doc;
}
public User() {
}
// getter and setter
}

3、调用lucene创建索引和搜索
lucene工具实现类LuceneManagerImpl.java:
package com.sillycat.easyview.core.service.impl;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.RAMDirectory;

import com.sillycat.easyview.core.service.LuceneManager;
import com.sillycat.easyview.plugin.commons.base.BaseManager;
import com.sillycat.easyview.plugin.commons.base.LuceneObject;
import com.sillycat.easyview.plugin.commons.utils.StringUtil;

public class LuceneManagerImpl extends BaseManager implements LuceneManager {

public static Log log = LogFactory.getLog(LuceneManagerImpl.class);

private RAMDirectory directory = null;

private ChineseAnalyzer analyzer = null;

private static final String INDEX_PATH = "D:\\lucene\\index";

private String indexPath;

public void init() {
   directory = new RAMDirectory();
   analyzer = new ChineseAnalyzer();
}

/**
* 搜索
* @param key     要搜索的KEY,比如找context字段 context
* @param search 要搜索的内容,比如找context中出现了 我爱你
* @param memory true 内存的索引,false 配置的路径的索引
*/
public Hits search(String key, String search,boolean memory) throws IOException,
    ParseException {
   IndexSearcher searcher = null;
   if (memory) {
    searcher = new IndexSearcher(directory);
   } else {
    IndexReader reader = IndexReader.open(this.getIndexPath());
    searcher = new IndexSearcher(reader);
   }
   QueryParser parser = new QueryParser(key, analyzer);
   return searcher.search(parser.parse(search));
}

/**
* 建立索引
* @param list   要建立索引的list
* @param memory    true 内存中建立索引,false 配置的路径上存放索引  
*/
public void buildIndex(List<LuceneObject> list,boolean memory)
    throws IOException {
   IndexWriter writer = null;
   if (memory) {
    writer = new IndexWriter(directory, analyzer, true);
   } else {
    writer = new IndexWriter(new File(this.getIndexPath()), analyzer,
      true);
   }
   Iterator<LuceneObject> iterator = list.iterator();
   Document doc = null;
   LuceneObject bo = null;
   while (iterator.hasNext()) {
    bo = (LuceneObject) iterator.next();
    doc = bo.buildindex();
    writer.addDocument(doc);
   }
   writer.optimize();
   writer.close();
}

public String getIndexPath() {
   if (StringUtil.isBlank(indexPath)) {
    indexPath = INDEX_PATH;
   }
   return indexPath;
}

public void setIndexPath(String indexPath) {
   this.indexPath = indexPath;
}

}

实现类的接口LuceneManager.java
package com.sillycat.easyview.core.service;

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

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.Hits;

import com.sillycat.easyview.plugin.commons.base.LuceneObject;

public interface LuceneManager {

public void init();

public Hits search(String key, String search,boolean memory) throws IOException,
    ParseException;

public void buildIndex(List<LuceneObject> list,boolean memory) throws IOException;

}

实现类的配置文件applicationContext-bean.xml:
<bean id="luceneManager"
    class="com.sillycat.easyview.core.service.impl.LuceneManagerImpl" autowire="byName" init-method="init">
    <!-- 在硬盘上存放索引的位置 -->
    <property name="indexPath" value="${lucene.indexPath}"/>
</bean>

配置文件easyview.properties
#lucene configiration
lucene.indexPath=D\:\\lucene\\index

4、单元测试
LuceneManagerTest.java单元测试:
package com.sillycat.easyview.core.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;

import com.sillycat.easyview.core.model.User;
import com.sillycat.easyview.plugin.commons.base.ServiceTestBase;

public class LuceneManagerTest extends ServiceTestBase {

private LuceneManager luceneManager;

protected void setUp() throws Exception {
   super.setUp();
   luceneManager = (LuceneManager) appContext.getBean("luceneManager");
}

protected void tearDown() throws Exception {
   super.tearDown();
}

public void testDumy() {
   assertTrue(true);
}

public void testSearch() throws Exception {
   luceneManager.init();
   List list = new ArrayList();
   User t1 = new User();
   t1.setId(Integer.valueOf("1"));
   t1.setLogonName("中文1");
   t1.setEmail("中文[email protected]");
   User t2 = new User();
   t2.setId(Integer.valueOf("2"));
   t2.setLogonName("中英文2");
   t2.setEmail("中英文[email protected]");
   list.add(t1);
   list.add(t2);
   //luceneManager.buildIndex(list,true);
   //Hits results = luceneManager.search("logonName", "中文1",true);
   luceneManager.buildIndex(list,false);
   Hits results = luceneManager.search("logonName", "中文1",false);
   assertEquals(1, results.length());

   Document doc = results.doc(0);
   assertEquals("中文1", doc.getField("logonName").stringValue());
   assertEquals("中文[email protected]", doc.getField("email").stringValue());

   //results = luceneManager.search("logonName", "中",true);
   results = luceneManager.search("logonName", "中",false);
   assertEquals(2, results.length());

}

}

你可能感兴趣的:(java,apache,单元测试,Lucene,mobile)