Solr 7自学笔记三(Solr 7.5利用SolrJ操作索引的增删改查)

1. Maven pom.xml

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.ccb

SolrJ

1.0-SNAPSHOT

UTF-8

       

org.apache.solr

solr-core

7.5.0

       

org.apache.solr

solr-test-framework

7.5.0

test

       

org.apache.solr

solr-solrj

7.5.0

       

org.junit.jupiter

junit-jupiter-api

5.2.0

test

       

org.apache.logging.log4j

log4j-api

2.11.1

2. log4j.xml

   

                    filepattern="${logPath}/%d{YYYYMMddHHmmss}-fargo.log">

3. 测试类(SolrJ的添加索引为例)

import org.apache.solr.client.solrj.SolrClient;

import org.apache.solr.client.solrj.SolrServerException;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

import org.apache.solr.common.SolrInputDocument;

import java.io.IOException;

import java.util.UUID;

public class Main {

public static void main(String[] args)throws IOException, SolrServerException {

System.out.println("Hello Solr...");

String urlString ="http://localhost:8983/solr/core1";

SolrClient solrClient =new HttpSolrClient.Builder(urlString).build();

System.out.println("INFO: Insert a document into Solr...");

SolrInputDocument doc =new SolrInputDocument();

doc.addField("id","8");

doc.addField("COMPANY","ebay");

doc.addField("LOCATION","Silicon Vally(硅谷)");

doc.addField("DESC-CO","ebay是一个与Amazon相似的电商网站,类似中国的阿里巴巴。开发Java,Web应用是很有必要的。");

doc.addField("uuid",UUID.randomUUID().toString());

solrClient.add(doc);

solrClient.commit();// periodically flush

        System.out.println("INFO: Insert the document into Solr Successfully");

}

}

4. 备注

Solr里面设置了uuid为唯一键。

你可能感兴趣的:(Solr 7自学笔记三(Solr 7.5利用SolrJ操作索引的增删改查))