zoukankan      html  css  js  c++  java
  • Solr简单使用

    1.添加索引

    		// 第一步:把solrJ的jar包添加到工程中。
    		// 第二步:创建一个SolrServer,使用HttpSolrServer创建对象。
    		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
    		// 第三步:创建一个文档对象SolrInputDocument对象。
    		SolrInputDocument document = new SolrInputDocument();
    		// 第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。
    		document.addField("id", "test001");
    		document.addField("item_title", "测试商品");
    		document.addField("item_price", "199");
    		// 第五步:把文档添加到索引库中。
    		solrServer.add(document);
    		// 第六步:提交。
    		solrServer.commit();
    

    2.删除

    		// 第一步:创建一个SolrServer对象。
    		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
    		// 第二步:调用SolrServer对象的根据id删除的方法。
    		solrServer.deleteById("1");
    		// 第三步:提交。
    		solrServer.commit();

                      SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");

                      solrServer.deleteByQuery("title:change.me");

                      solrServer.commit();


    3.查询

    		SolrServer solrServer = new HttpSolrServer("http://192.168.25.154:8080/solr");
    		// 第二步:创建一个SolrQuery对象。
    		SolrQuery query = new SolrQuery();
    		// 第三步:向SolrQuery中添加查询条件、过滤条件。。。
    		query.setQuery("*:*");
    		// 第四步:执行查询。得到一个Response对象。
    		QueryResponse response = solrServer.query(query);
    		// 第五步:取查询结果。
    		SolrDocumentList solrDocumentList = response.getResults();
    		System.out.println("查询结果的总记录数:" + solrDocumentList.getNumFound());
    		// 第六步:遍历结果并打印。
    		for (SolrDocument solrDocument : solrDocumentList) {
    			System.out.println(solrDocument.get("id"));
    			System.out.println(solrDocument.get("item_title"));
    			System.out.println(solrDocument.get("item_price"));
    		}
    

      

  • 相关阅读:
    Cookies和Session的定义与区别
    selenium元素定位篇
    http协议基础教程
    selenium自动化基础知识
    数组基础练习
    软件生命周期及其相关文档
    作业1
    作业2
    第四次个人作业关于微软必应词典客户端 的案例分析
    自我期许
  • 原文地址:https://www.cnblogs.com/zhoucx66/p/9299264.html
Copyright © 2011-2022 走看看