solr 高亮、分页、排序、过滤查询

  高亮、分页、排序、过滤查询

           //关键字主查询 高亮查询
            HighlightQuery query = new SimpleHighlightQuery();
            Criteria name = new Criteria("name").is(name);
            query.addCriteria(name);

            //条件过滤查询,对结果过滤
            if (type!==""){
                FilterQuery filterQuery=new SimpleFilterQuery();
                Criteria criteriaType = new 
                Criteria("type").is(type);
                filterQuery.addCriteria(criteriaType);
                query.addFilterQuery(filterQuery);
            }

            //高亮字段
            HighlightOptions highlightOptions = new HighlightOptions();
            highlightOptions.addField("name");
            highlightOptions.addField("info");

            highlightOptions.setSimplePrefix("");
            highlightOptions.setSimplePostfix("");
            query.setHighlightOptions(highlightOptions);
            //分页
            query.setPageRequest(PageRequest.of(pageNum-1,pageSize));
            //排序
            query.addSort(Sort.by("xxx").descending());
            Page highlightPage = solrTemplate.queryForHighlightPage(solr_core, query, XXX.class);

            //解析高亮结果
            List> highlighted = ((HighlightPage) highlightPage).getHighlighted();
            for (HighlightEntry itemHighlightEntry : highlighted) {
                //包含高亮文档
                XXX item = itemHighlightEntry.getEntity();
                //高亮的数据
                List highlights = itemHighlightEntry.getHighlights();
                if(highlights != null && highlights.size() >0) {
                    //多字段高亮
                    for (int i = 0; i < highlights.size() ; i++) {
                        //域的高亮数据
                        HighlightEntry.Highlight highlight = highlights.get(i);
                        Field field = highlight.getField();
                        if (field.toString().equals("name")){
                            String title = highlight.getSnipplets().get(0);
                            item.setName(title);
                        }
                        else {
                            String title = highlight.getSnipplets().get(0);
                            item.setInfo(title);
                        }
                    }
                }
            }
            List content = highlightPage.getContent();

 facet统计

SimpleFacetAndHighlightQuery simpleFacetAndHighlightQuery = new SimpleFacetAndHighlightQuery();
simpleFacetAndHighlightQuery.addCriteria(new SimpleStringCriteria("*:*"));

       
FilterQuery filterQuery = new SimpleFilterQuery();
Criteria criteriaType = new Criteria("域字段").is("参数");
filterQuery.addCriteria(criteriaType);
simpleFacetAndHighlightQuery.addFilterQuery(filterQuery);

FacetOptions facetOptions = new FacetOptions();
//设置分类的域字段
facetOptions.addFacetOnField("域字段");
//分类返回的条数,默认为10
facetOptions.setFacetLimit(20);
simpleFacetAndHighlightQuery.setFacetOptions(facetOptions);
simpleFacetAndHighlightQuery.setPageRequest(PageRequest.of(0, 100));
Page<存入sorl对应的类> highlightPage = solrTemplate.queryForFacetAndHighlightPage(solr_core, simpleFacetAndHighlightQuery, "存入sorl对应的类".class);
Page facetResultPage = ((FacetAndHighlightPage<存入sorl对应的类>) highlightPage).getFacetResultPage("域字段");

你可能感兴趣的:(solr,java,lucene)