zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    人生没有彩排,每天都是现场直播!
  • 相关阅读:
    使用python,批量生产条形码
    excel——VlookUp函数的使用
    MQTT消息队列压力测试
    shell脚本中,关于if,以及条件判断
    linux下的echo
    python对文件操作 r w a 文件复制/修改
    使用Appium进行iOS的真机自动化测试
    pycharm 解决PEP8问题,配置autopep8到菜单栏
    Python中list的合并
    Centos最小化安装后,不能使用yum命令的解决办法
  • 原文地址:https://www.cnblogs.com/northern-light/p/10497502.html
Copyright © 2011-2022 走看看