프로그램/JAVA

[SolrJ] spring boot + solrj 연동 및 사용하기

프뚜 2022. 11. 24. 13:24
728x90
반응형
SMALL

안녕하세요!

프뚜(프로그래머 뚜)입니다!

 

solrJ를 통해서 solrJ와 통신할 수 있습니다.


[개발 환경]

 - OS: windows 10 64bit

 - JAVA: 1.8


1. solrj > gradle 추가하기
implementation 'org.apache.solr:solr-solrj'


2. Solrj 사용하기
// 2022.11.24[프뚜]: 주키퍼(3개)
private String[] zookeepers = {
        "192.168.0.100:7777",
        "192.168.0.101:7777",
        "192.168.0.102:7777"
};

// 2022.11.24[프뚜]: 연동
private CloudSolrClient solrClient = new CloudSolrClient.Builder(
        Arrays.asList(zookeepers)
        , Optional.empty()).build();

@Override
public void run(ApplicationArguments args) throws Exception {
    solrClient.setDefaultCollection("collections");

    // 2022.11.23[프뚜]: /select
    Map<String, Object> getParams = new HashMap();
    getParams.put("q", "테스트");
    getParams.put("wt", "json");
    get("/select", getParams);

    // 2022.11.23[프뚜]: /delete
    Map<String, Object> deleteParams = new HashMap();
    deleteParams.put("commit", "true");
    deleteParams.put("wt", "json");
    post("/delete", deleteParams, "application/xml", "<delete><query>*:*</query></delete>");

    // 2022.11.23[프뚜]: /tag
    Map<String, Object> tagParams = new HashMap();
    tagParams.put("commit", "true");
    tagParams.put("wt", "json");
    post("/tag", tagParams, "text/plain", "프뚜");

    // 2022.11.23[프뚜]: /update
    Map<String, Object> updateParams = new HashMap();
    updateParams.put("commit", "true");
    updateParams.put("wt", "json");
    post("/update", updateParams, "application/json", "{\"ID\": \"ssjeong\"}");
}

// 2022.11.24[프뚜]: get
private void get(String path, Map params) throws Exception {
    SolrQuery query = new SolrQuery();

    Set<String> keys = new HashSet();
    for (String key : keys) {
        query.add(key, (String) params.get(key));
    }

    QueryRequest request = new QueryRequest(query);
    request.setMethod(SolrRequest.METHOD.GET);

    // 2022.11.23[프뚜]: http://[host]:[port]/[collections]/[path]
    request.setPath(path);

    QueryResponse response = request.process(solrClient);
    response.jsonStr();
}

// 2022.11.24[프뚜]: post
private void post(String path, Map params, String contentType, String body) throws Exception {
    ContentStreamBase.StringStream contentStreamBase = new ContentStreamBase.StringStream(body);
    contentStreamBase.setContentType(contentType);

    ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(path);
    request.addContentStream(contentStreamBase);

    Set<String> keys = new HashSet();
    for (String key : keys) {
        request.setParam(key, (String) params.get(key));
    }

    UpdateResponse response = request.process(solrClient);
    response.jsonStr();
}

주키퍼 주소(여러 개)를 이용해서 solrClient로 접속합니다.

get은 queryString을 통해 데이터를 주고 받기 때문에 SolrQuery에 값을 넣은 후 QueryRequest를 사용합니다.

post는 ContentStreamBase를 통해서 Header에 맞는 데이터 규격을 Body에 넣을 수 있습니다.


소스는 간단히자만, 영어가 까막눈인 프뚜는 Document를 분석하는데 많이 고생했습니다. 다른 분들에게 도움이 되길 바랍니다.

 

자세한 내용은 소스코드를 통해 확인해보실 수 있습니다. (제목과 Git Comment는 같습니다.)

https://github.com/JeongSeongSoo/spring-tistory

 

GitHub - JeongSeongSoo/spring-tistory

Contribute to JeongSeongSoo/spring-tistory development by creating an account on GitHub.

github.com

728x90
반응형
LIST