根据多个字段搜索

package loaderman.search;

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

import loaderman.entity.Article;
import loaderman.util.LuceneUtil;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
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.junit.Test;


/**
 * 演示Lucene中,根据多个字段搜索*/
public class ArticleDao {
    /**
     * 增加document对象索引库中
     */
    @Test
    public void add() throws Exception{
        Article article = new Article(1,"学习","额外额外为",10);
        //Article article = new Article(2,"学习","额外it为",20);
        //Article article = new Article(3,"学习","额it额",20);
        //Article article = new Article(4,"学习","嗯嗯it嗯嗯",30);
        Document document = LuceneUtil.javabean2document(article);
        IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory()    ,LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
        indexWriter.addDocument(document);
        indexWriter.close();
    }




    @Test
    public void findAll() throws Exception{
        String keywords = "机构";
        List<Article> articleList = new ArrayList<Article>();

        //单字段搜索
        //QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(),"title",LuceneUtil.getAnalyzer());

        //多字段搜索,好处:搜索的范围大,最大限度匹配搜索结果
        QueryParser queryParser =
                new MultiFieldQueryParser(
                        LuceneUtil.getVersion(),
                        new String[]{"content","title"},
                        LuceneUtil.getAnalyzer());


        Query query = queryParser.parse(keywords);
        IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory());

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

        for(int i=0;i<topDocs.scoreDocs.length;i++){
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            int no = scoreDoc.doc;
            Document document = indexSearcher.doc(no);
            Article article = (Article) LuceneUtil.document2javabean(document,Article.class);
            articleList.add(article);
        }
        for(Article a : articleList){
            System.out.println(a);
        }
    }
}
package loaderman.entity;

/**
 * 文章
 */
public class Article {
    private Integer id;//编号
    private String title;//标题
    private String content;//内容
    private Integer count;//字数
    public Article(){}
    public Article(Integer id, String title, String content, Integer count) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.count = count;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public Integer getCount() {
        return count;
    }
    public void setCount(Integer count) {
        this.count = count;
    }
    @Override
    public String toString() {
        return "编号:" + id + "标题:" + title + "内容:" + content + "字数: " + count;
    }
}

 

posted on 2018-12-03 17:29  LoaderMan  阅读(447)  评论(0编辑  收藏  举报

导航