SolrJ查询语法总结

//AND 并且
    SolrQuery params = new SolrQuery("name:apple AND manu:inc");
    
    //OR 或者
    params.setQuery("name:apple OR manu:apache");
    //空格 等同于 OR
    params.setQuery("name:server manu:dell");
    
    //params.setQuery("name:solr - manu:inc");
    //params.setQuery("name:server + manu:dell");
    
    //查询name包含solr apple
    params.setQuery("name:solr,apple");
    //manu不包含inc
    params.setQuery("name:solr,apple NOT manu:inc");
    
    //50 <= price <= 200
    params.setQuery("price:[50 TO 200]");
    params.setQuery("popularity:[5 TO 6]");
    //params.setQuery("price:[50 TO 200] - popularity:[5 TO 6]");
    //params.setQuery("price:[50 TO 200] + popularity:[5 TO 6]");
    
    //50 <= price <= 200 AND 5 <= popularity <= 6
    params.setQuery("price:[50 TO 200] AND popularity:[5 TO 6]");
    params.setQuery("price:[50 TO 200] OR popularity:[5 TO 6]");
    
    //过滤器查询,可以提高性能 filter 类似多个条件组合,如and
    //params.addFilterQuery("id:VA902B");
    //params.addFilterQuery("price:[50 TO 200]");
    //params.addFilterQuery("popularity:[* TO 5]");
    //params.addFilterQuery("weight:*");
    //0 < popularity < 6  没有等于
    //params.addFilterQuery("popularity:{0 TO 6}");
    
    //排序
    params.addSortField("id", ORDER.asc);
    //分页:start开始页,rows每页显示记录条数
    //params.add("start", "0");
    //params.add("rows", "200");
    //params.setStart(0);
    //params.setRows(200);
    
    //设置高亮
    params.setHighlight(true); // 开启高亮组件
    params.addHighlightField("name");// 高亮字段
    params.setHighlightSimplePre("<font color='red'>");//标记,高亮关键字前缀
    params.setHighlightSimplePost("</font>");//后缀
    params.setHighlightSnippets(1);//结果分片数,默认为1
    params.setHighlightFragsize(1000);//每个分片的最大长度,默认为100
 
    //分片信息
    params.setFacet(true)
        .setFacetMinCount(1)
        .setFacetLimit(5)//段
        .addFacetField("name")//分片字段
        .addFacetField("inStock");
    
    //params.setQueryType("");
    
    try {
        QueryResponse response = server.query(params);
        
        /*List<Index> indexs = response.getBeans(Index.class);
        for (int i = 0; i < indexs.size(); i++) {
            fail(indexs.get(i));
        }*/
        
        //输出查询结果集
        SolrDocumentList list = response.getResults();
        fail("query result nums: " + list.getNumFound());
        for (int i = 0; i < list.size(); i++) {
            fail(list.get(i));
        }
        
        //输出分片信息
        List<FacetField> facets = response.getFacetFields();
        for (FacetField facet : facets) {
            fail(facet);
            List<Count> facetCounts = facet.getValues();
            for (FacetField.Count count : facetCounts) {
                System.out.println(count.getName() + ": " + count.getCount());
            }
        }
    } catch (SolrServerException e) {
        e.printStackTrace();
    }

你可能感兴趣的:(SolrJ查询语法总结)