当前位置: 代码迷 >> Java相关 >> lucene不能单个字母搜寻
  详细解决方案

lucene不能单个字母搜寻

热度:92   发布时间:2016-04-22 20:59:48.0
lucene不能单个字母搜索,
lucene不能单个字母搜索,单个汉字又可以搜索到,就是当输入一个 a 时,明明有这条数据的就是搜索不到,求lucene高手指教啊、
------解决方案--------------------
与建索引时使用的分词器有关,一般的分词器不会将一个单词按照字母分开,这个需求可能需要你自己实现一个分词器。
------解决方案--------------------
这个的确和分词器有关系,但是 单字母,单汉字,应该可以分出来的,

你用什么分词器,分词器的算法是很么,你了解一下

其实我感觉,分词,分词,如果单字母,单字的,这样的搜索意义不大

http://search.gongkong.com/search.aspx  你看下我的这个效果
------解决方案--------------------
试一下我写的这个程序是可以的哦!!
一、导入jar包



二、LuenctTest.java代码

package com.sphinx.util;

import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;


public class LuenceTest {
 
 private static Analyzer ANALYZER=new IKAnalyzer();//声明一个中文分词器
 private static File  path_index=null;
 private static IndexWriter indexWriter=null;
 
 
 
 public static void createIndexFile(){
  
  try {
   path_index=new File("./indexDIR/");
   if(!path_index.exists()){
    path_index.mkdirs();
   }
   //创建索引目录
   Directory dir = SimpleFSDirectory.open(path_index);
   
   //建立索引创建类
   IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_36, ANALYZER);
   indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);//总是重新创建索引
   //声明一个索引库
   indexWriter=new IndexWriter(dir,indexWriterConfig);
   
   
   Document doc=new Document();
   doc.add(new Field("id","1",Store.YES,Index.NOT_ANALYZED_NO_NORMS));//id不分词
   doc.add(new Field("title","我们是中国人",Store.YES,Index.ANALYZED));
   doc.add(new Field("content","我们是是是是是是是是",Store.YES,Index.ANALYZED));
   //放入索引库
   indexWriter.addDocument(doc);
   System.out.println("创建了"+indexWriter.numDocs()+"索引");
  } catch (IOException e) {
   e.printStackTrace();
   
  }finally{
   
   if(indexWriter!=null){
    try {
     
     indexWriter.close();
     
    } catch (CorruptIndexException e) {
     e.printStackTrace();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
 
 
 
 public static void search(String queryStr){
  try {
   //找到索引目录
   IndexReader reader=IndexReader.open(FSDirectory.open(path_index));
   //创建搜索类
   IndexSearcher searcher=new IndexSearcher(reader);
   //搜索条件
   Query query=IKQueryParser.parse("title", queryStr);
   Query query1=IKQueryParser.parse("content", queryStr);
   TopScoreDocCollector collector=TopScoreDocCollector.create(100, true);
   searcher.search(query, collector);
   searcher.search(query1, collector);
   ScoreDoc[] hits=collector.topDocs().scoreDocs;
   if(hits.length>0){
    for(int i=0;i<hits.length;i++){
     //内部编号
     int id=hits[i].doc;
     //根据文档id找到文档
     Document mydoc=searcher.doc(id);
     System.out.println(mydoc.get("id")+mydoc.get("content"));
    }
    
    System.out.println("检索词:\t"+queryStr+"\t共找到"+hits.length+"条记录");
   }
  } catch (CorruptIndexException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  相关解决方案