为什么创建索引后查询没有结果?

wslk857208 发布于 2013/08/13 22:33
阅读 339
收藏 0

如题运行createIndex后再运行searcehIndex没有打印结果


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.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;


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


/**
 * Created with IntelliJ IDEA.
 * To change this template use File | Settings | File Templates.
 */
public class LuceneUtil {


    Directory directory;


    public LuceneUtil() throws IOException {
        directory = FSDirectory.open(new File("D:/indexDir/"));
    }


    @Test
    public void createIndex() {
        IndexWriter indexWriter = getIndexWriter();
        Article article = new Article();
        article.setId(1);
        article.setTitle("这里是标题");
        article.setContent("这里是内容!");
        Document doc = new Document();
        doc.add(new IntField("id", article.getId(), Field.Store.YES));
        doc.add(new StringField("title", article.getTitle(), Field.Store.YES));
        doc.add(new StringField("content", article.getContent(), Field.Store.YES));
        try {
            indexWriter.addDocument(doc);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                indexWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }


    @Test
    public void searchIndex() {
        IndexSearcher indexSearcher = getIndexSearcher();
        Term term = new Term("content", "内");
        TermQuery termQuery = new TermQuery(term);
        try {
            TopDocs topDocses = indexSearcher.search(termQuery, 3);
            ScoreDoc[] scorers = topDocses.scoreDocs;
            List<Article> articles = new ArrayList<>();
            for (ScoreDoc a : scorers) {
                Document doc = indexSearcher.doc(a.doc);
                Article article = new Article();
                article.setId(Integer.parseInt(doc.get("id")));
                article.setTitle(doc.get("title"));
                article.setContent(doc.get("content"));
                articles.add(article);
                doc.get("content");
            }
            for (Article a : articles) {
                System.out.println(a.getId());
                System.out.println(a.getTitle());
                System.out.println(a.getContent());
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private IndexWriter getIndexWriter() {
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_44, analyzer);
        IndexWriter indexWriter = null;
        try {
            indexWriter = new IndexWriter(directory, conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return indexWriter;
    }


    private IndexSearcher getIndexSearcher() {
        IndexReader indexReader = null;
        try {
            indexReader = DirectoryReader.open(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new IndexSearcher(indexReader);
    }
}

@红薯 @虫虫

加载中
0
wslk857208
wslk857208
找到原因了,是StringField
Tanweijie
Tanweijie
回复 @yaolifei : 因为StringField创建索引的时候不进行分词操作。使用TextField就可以了。
onlyfish
onlyfish
StringField怎么了 兄弟倒是说下原因啊
返回顶部
顶部