搜索结果排序

什么是搜索结果排序

    搜索结果是按某个或某些字段高低排序来显示的结果

影响网站排名的先后的有多种

     head/meta/

     网页的标签整洁

     网页执行速度

     采用div+css

     。。。。。。

Lucene中的显示结果次序与相关度得分有关

    ScoreDoc.score;

    默认情况下,Lucene是按相关度得分排序的,得分高排在前,得分低排在后

    如果相关度得分相同,按插入索引库的先后次序排序

Lucene中的设置相关度得分

IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory(),LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
        document.setBoost(20F);
        indexWriter.addDocument(document);
        indexWriter.close();

Lucene中按单个字段排序

Sort sort = new Sort(new SortField("id",SortField.INT,true));
    TopDocs topDocs = indexSearcher.search(query,null,1000000,sort);

Lucene中按多个字段排序

Sort sort = new Sort(new SortField("count",SortField.INT,true),new SortField("id",SortField.INT,true));
        TopDocs topDocs = indexSearcher.search(query,null,1000000,sort);
package loaderman.sort;

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.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 ArticleDao1 {
    /**
     * 增加document对象索引库中
     */
    @Test
    public void add() throws Exception{

        Article article = new Article(3,"学习","学习是有人打卡",30);


        Document document = LuceneUtil.javabean2document(article);
        IndexWriter indexWriter = new IndexWriter(LuceneUtil.getDirectory()    ,LuceneUtil.getAnalyzer(),LuceneUtil.getMaxFieldLength());
        //人工设置该document的得分
        //document.setBoost(100F);
        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(),"content",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对象的评分
            float score = scoreDoc.score;
            System.out.println("score=" + score);

            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.sort;

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.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.junit.Test;

/**
 * 演示Lucene中,根据单个或多个字段排序
 */
public class ArticleDao2 {
    /**
     * 增加document对象索引库中
     */
    @Test
    public void add() throws Exception{

        Article article = new Article(4,"学习","打算 大打算大声道",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(),"content",LuceneUtil.getAnalyzer());
        Query query = queryParser.parse(keywords);
        IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory());


        //按得分度高低排序
        //TopDocs topDocs = indexSearcher.search(query,100);

        //创建排序对象
        //参数一:id表示依据document对象中的哪个字段排序,例如:id
        //参数二:SortField.INT表示document对象中该字段的类型,以常量方式书写
        //参数三:true表示降序,类似于order by id desc
        //参数三:false表示升序,类似于order by id asc
        //Sort sort = new Sort(new SortField("id",SortField.INT,false));

        //按count字段的降序排列,如果count字段相同的话,再按id的升序排序
        Sort sort = new Sort(
                new SortField("count",SortField.INT,true),
                new SortField("id",SortField.INT,false));

        //sort表示排序的条件
        TopDocs topDocs = indexSearcher.search(query,null,100,sort);

        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);
        }
    }
}

 

 

 

什么是搜索结果排序

    搜索结果是按某个或某些字段高低排序来显示的结果

52影响网站排名的先后的有多种

     head/meta/

     网页的标签整洁

     网页执行速度

     采用div+css

     。。。。。。

53Lucene中的显示结果次序与相关度得分有关

    ScoreDoc.score;

    默认情况下,Lucene是按相关度得分排序的,得分高排在前,得分低排在后

    如果相关度得分相同,按插入索引库的先后次序排序

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

导航