[elasticsearch] Java API : Get

Elastic/Elasticsearch 2013. 4. 9. 12:25

본 문서는 개인적인 테스트와 elasticsearch.org 그리고 community 등을 참고해서 작성된 것이며,

정보 교환이 목적입니다.


잘못된 부분에 대해서는 지적 부탁 드립니다.

(예시 코드는 성능 및 보안 검증이 되지 않았습니다.)



[elasticsearch java api 리뷰]

원문 링크 : http://www.elasticsearch.org/guide/reference/java-api/get/


- 이 API는 index 의 id(_id) 를 기반으로 json 형식의 문서를 구할 수 있습니다.


예제는 아래 문서에서 생성한 데이터로 테스트 합니다.

http://jjeong.tistory.com/792


GetResponse response = client.prepareGet("facebook", "post", "2")

        .execute()

        .actionGet();

log.debug("{}", response.getId());

log.debug("{}", response.getSource());


response = client.prepareGet("facebook", "post", "1")

        .setOperationThreaded(false)

        .execute()

        .actionGet();

log.debug("{}", response.getId());

log.debug("{}", response.getSource());

- 보시면 매우 간단 합니다.

- REST URL 방식으로 고치면 http://localhost:9200/facebook/post/2 와 같은 형식을 갖습니다.


좀 더 자사한 내용은 아래 링크 참고 바랍니다.

http://www.elasticsearch.org/guide/reference/api/get/


[.setOperationThreaded 옵션]

- 이 옵션의 기본 값은 true 로 설정이 되어 있습니다.

- operation 에 대한 threading model 을 지원.

- 설명이 어려워서 일단 쉽게 이해를 돕기 위해 소스코드를 좀 봅시다.


[SearchOperationThreading.java]

    /**

     * No threads are used, all the local shards operations will be performed on the calling

     * thread.

     */

    NO_THREADS((byte) 0),

    /**

     * The local shards operations will be performed in serial manner on a single forked thread.

     */

    SINGLE_THREAD((byte) 1),

    /**

     * Each local shard operation will execute on its own thread.

     */

    THREAD_PER_SHARD((byte) 2);


[ThreadingModel.java]

    NONE((byte) 0),

    OPERATION((byte) 1),

    LISTENER((byte) 2),

    OPERATION_LISTENER((byte) 3);


    /**

     * <tt>true</tt> if the actual operation the action represents will be executed

     * on a different thread than the calling thread (assuming it will be executed

     * on the same node).

     */

    public boolean threadedOperation() {

        return this == OPERATION || this == OPERATION_LISTENER;

    }


    /**

     * <tt>true</tt> if the invocation of the action result listener will be executed

     * on a different thread (than the calling thread or an "expensive" thread, like the

     * IO thread).

     */

    public boolean threadedListener() {

        return this == LISTENER || this == OPERATION_LISTENER;

    }


- 주석을 보시면 이해가 되시죠?

- 즉 false이면 자긴껀 자기가 실행 하고, true 이면 thread 를 fork 해서 넘겨준다고 보면 되겠내요.


※ 커뮤니티에 올라와 있는 것들을 보면 아래와 같이 사용하는걸 권장 하는 것 같습니다.

- read operation : setThreadOperation(true) // 즉 선언 하지 않아도 되구요.

- insert/update/delete operation : setThreadOperation(false)


: