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"));
    		}
    

      

  • 相关阅读:
    PHP 速度测试函数
    ajax的简单应用之快速入门
    PHP漏洞详解
    jQuery Ajax 实例 全解析
    2007年最后的一天
    近来心情格外的郁闷
    使用 Ajax 实现 lightbox
    GridView删除,编辑应用
    完美的wsus客户端注册表文件
    VISTA桌面显示Internet Explorer
  • 原文地址:https://www.cnblogs.com/zhoucx66/p/9299264.html
Copyright © 2011-2022 走看看