solr测试


public class SolrTest extends TestCase {
    public void testSolrJ() throws Exception {
        SolrServer solrServer = new HttpSolrServer(
                "http://192.168.1.111:8080/solr");
        // 创建一个文档对象
        SolrInputDocument document = new SolrInputDocument();
        // 添加域
        document.addField("id", "solrtest01");
        document.addField("item_title", "测试商品");
        document.addField("item_sell_point", "卖点");
        // 添加到索引库
        solrServer.add(document);
        // 提交
        solrServer.commit();

    }
}

pom.xml

  <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>3.8.1version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>org.apache.solrgroupId>
      <artifactId>solr-solrjartifactId>
      <version>4.10.1version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-log4j12artifactId>
      <version>1.7.7version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>commons-logginggroupId>
      <artifactId>commons-loggingartifactId>
      <version>1.1.3version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>org.apache.commonsgroupId>
      <artifactId>commons-lang3artifactId>
      <version>3.3.2version>
      <scope>compilescope>
    dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.4.2version>
      <scope>compilescope>
    dependency>
    public void testQuery() throws Exception {
        //创建连接
        SolrServer solrServer = new HttpSolrServer("http://192.168.1.111:8080/solr");
        //创建一个查询对象
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        //执行查询
        QueryResponse response = solrServer.query(query);
        //取查询结果
        SolrDocumentList solrDocumentList = response.getResults();
        for (SolrDocument solrDocument : solrDocumentList) {
            System.out.println(solrDocument.get("id"));
            System.out.println(solrDocument.get("item_title"));
            System.out.println(solrDocument.get("item_sell_point"));
        }
    }

你可能感兴趣的:(solr,solr测试,Java,nginx,zookeeper)