Lucene初学 搜索结果一直为0 求大神帮忙看一下如何解决

xaxqq 2015-07-09 03:46:26
版本是4.3
package cn.news.test;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
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.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import cn.news.domain.News;

@SuppressWarnings("deprecation")
public class LuceneUtil {
public static String INDEX_PATH = "E:\\News\\index";

public static IKAnalyzer analyzer = new IKAnalyzer(true);

public static synchronized boolean createIndex(LuceneData data)
throws Exception {

Directory index = FSDirectory.open(new File(INDEX_PATH));
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_43,
analyzer);

Document doc = getDocument(data);

IndexWriter w = new IndexWriter(index, config);
w.addDocument(doc);
System.out.println("共有索引" + w.numDocs() + "个");

w.commit();

return true;
}

private static Document getDocument(LuceneData data) {
Document doc = new Document();
doc.add(new Field("id", data.getId(), Store.YES, Index.NOT_ANALYZED));
doc.add(new Field("title", data.getTitle(), Store.YES, Index.ANALYZED));
doc.add(new Field("introduction", data.getIntroduction(), Store.YES,
Index.ANALYZED));
doc.add(new Field("content", data.getContent(), Store.YES,
Index.ANALYZED));
return doc;
}

public void search(String str) throws Exception {
String queryString = str;

analyzer.setUseSmart(true);

String[] fields = { "title", "introduction", "content" };
Occur[] occurs = { Occur.SHOULD, Occur.SHOULD, Occur.SHOULD };

Directory directory = FSDirectory.open(new File(INDEX_PATH));
IndexReader reader = DirectoryReader.open(directory);

IndexSearcher indexSearcher = new IndexSearcher(reader);
Query query = MultiFieldQueryParser.parse(Version.LUCENE_43,
queryString, fields, occurs, analyzer);

TopDocs topDocs = indexSearcher.search(query, 10000);

System.out.println("共有【" + topDocs.totalHits + "】条结果");

for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int id = scoreDoc.doc;
Document doc = indexSearcher.doc(id);
printDoc(doc);
}
}

public void printDoc(Document doc) {
System.out.println("id\t=\t" + doc.get("id"));
System.out.println("title\t=\t" + doc.get("title"));
System.out.println("introduction\t=\t" + doc.get("introduction"));
System.out.println("content\t=\t" + doc.get("content"));
}

@Test
public void test() throws Exception {
LuceneData data = new LuceneData("1", "新闻标题 ", "新闻导语 ", "新闻内容");
createIndex(data);
search("新闻");
}
}
...全文
46 1 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
hellodcj 2015-07-09
解决思路:使用luke工具,查看索引的结果是否正确,分词工具的差异有可能导致索引结果的不同
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-07-09 03:46
社区公告
暂无公告