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();
        }
    }
    人生没有彩排,每天都是现场直播!
  • 相关阅读:
    mysql主从复制的一些东西的整理
    (转载)[我只是认真]聊聊工匠情怀
    Redis运维的一些常用的命令总结
    关于mysql和Apache以及nginx的监控脚本怎么写会比较好的记录
    使用linux的nc来进行文件的传输
    nc检测端口是否正常服务的一个命令
    二维数组去除重复值和array_unique函数
    MySQL的备份的一些策略和方法的总结
    一些容易忘记的小知识点
    关于php多线程的记录
  • 原文地址:https://www.cnblogs.com/northern-light/p/10497502.html
Copyright © 2011-2022 走看看