• solr查询 操作


    package com.jy.util;
    /**
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.impl.HttpSolrClient;
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.common.SolrDocument;
    import org.apache.solr.common.SolrDocumentList;
    import org.apache.solr.common.SolrInputDocument;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    */
    public class SolrTest {
    
        /**
            solrj 查询示例
        */
    
        public void add() throws IOException, SolrServerException {
            final String solrUrl = "";
            HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
            SolrInputDocument document = new SolrInputDocument();
            document.addField("id", "c0002");
            document.addField("title_ik", "使用solrJ添加的文档");
            document.addField("content_ik", "solrj文档的内容");
            document.addField("product_name", "solrJ商品名称");
            client.add(document);
            client.commit();
        }
    
        public void search() throws IOException, SolrServerException {
            final String solrUrl = "";
            HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
            SolrQuery solrQuery = new SolrQuery();
            solrQuery.set("q","id:id001");
            QueryResponse response = client.query(solrQuery);
            SolrDocumentList results = response.getResults();
            for(SolrDocument document:results){
                String id = document.get("id").toString();
                String productName = document.get("product_name").toString();
                System.out.println(id);
                System.out.println(productName);
            }
        }
    
        public void search2() throws IOException, SolrServerException {
            final String solrUrl = "";
            HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
            SolrQuery solrQuery = new SolrQuery();
            solrQuery.setQuery("精致真皮手表");
            solrQuery.setFilterQueries("product_name:手表");
            solrQuery.setSort("id",SolrQuery.ORDER.desc);
            solrQuery.setStart(0);
            solrQuery.setRows(30);
            // 设置显得的域的列表
            solrQuery.setFields("id", "title_ik", "content_ik","product_name");
            // 设置默认搜索域
            solrQuery.set("df", "product_name");
            // 设置高亮
            solrQuery.setHighlight(true);
            solrQuery.addHighlightField("product_name");
            solrQuery.setHighlightSimplePre("<em>");
            solrQuery.setHighlightSimplePost("</em>");
            QueryResponse response = client.query(solrQuery);
            // 查询结果
            SolrDocumentList results = response.getResults();
            // 查询结果总数
            long totalCount = results.getNumFound();
            System.out.println("查询结果总数:" + totalCount);
    
            Map<String, Map<String, List<String>>> maps = response.getHighlighting();
            SolrDocumentList documentList = response.getResults();
            //7、遍历查询结果
            for (SolrDocument solrDocument : documentList) {
                System.out.print(solrDocument.get("id")+" ");
                List<String> titleList = maps.get(solrDocument.get("id")).get("item_title");
                if (titleList !=null && titleList.size()>0) {
                    System.out.print(titleList.get(0));
                }
                System.out.println();
            }
        }
    
        public void delete() throws IOException, SolrServerException {
            final String solrUrl = "";
            HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(60000).build();
            client.deleteById("id001");
            client.commit();
        }
    }
    人生没有彩排,每天都是现场直播!
  • 相关阅读:
    同一个表中今天的数据与昨天的数据合并,并制定列
    ◎UrlEncode 与 ◎UrlDeCode对应Lotusscript
    ExtJS初级教程之ExtJS Grid(三)
    ExtJS初级教程之ExtJS Tree(一)
    Collections常用的静态方法浅析之排序:sort(List list)
    ExtJS初级教程之ExtJS Tree(三)
    不能被复制的字符:'/u0000'
    ExtJS初级教程之ExtJS Tree(二)
    urlwriterfilter地址栏的伪装
    ExtJS初级教程之ExtJS Grid(一)
  • 原文地址:https://www.cnblogs.com/northern-light/p/10497502.html
Copyright © 2020-2023  润新知