'Groovy'에 해당되는 글 3건

  1. 2017.10.31 [Groovy] foreach..break 테스트
  2. 2015.08.24 [Elasticsearch] java.lang.ClassNotFoundException: groovy.lang.GroovyClassLoader
  3. 2015.08.17 [Elasticsearch] Scripting 을 이용한 Field Value 조작하기.

[Groovy] foreach..break 테스트

ITWeb/개발일반 2017. 10. 31. 16:03

[foreach.groovy]

def list = ["A", "B", "C", "D"]


list.each {

  println it


  if ( it.indexOf("B") > -1 ) return true

}

println "---------------"

list.any {

  println it


  if ( it.indexOf("B") > -1 ) return true

}


[Result]

A

B

C

D

---------------

A

B


:

[Elasticsearch] java.lang.ClassNotFoundException: groovy.lang.GroovyClassLoader

Elastic/Elasticsearch 2015. 8. 24. 18:19

이 에러는 elasticsearch 프로젝트를 하나 만들어서 개발 하다 보면 경험하게 되는 메시지 입니다.


[에러]

java.lang.ClassNotFoundException: groovy.lang.GroovyClassLoader


이것은 groovy-all 과 lucene-expressions 에 대한 dependency 때문에 발생을 하는 것인데요.
이런 에러가 보기 싫으신 분들은 생성한 프로젝트의 pom.xml 에 아래 dependency 를 추가해 주시면 됩니다.


[pom.xml]

    <dependency>

      <groupId>org.codehaus.groovy</groupId>

      <artifactId>groovy-all</artifactId>

      <version>2.3.2</version>

      <scope>compile</scope>

      <optional>true</optional>

    </dependency>


    <dependency>

      <groupId>org.apache.lucene</groupId>

      <artifactId>lucene-expressions</artifactId>

      <version>4.10.2</version>

      <scope>compile</scope>

      <optional>true</optional>

    </dependency>

※ 여기서 주의 하셔야 할 점은 version 에 맞춰서 정보를 넣어 주셔야 한다는 것입니다.

※ elasticsearch 는 lucene 기반이기 때문에 version 은 꼭 확인 하셔야 합니다.


:

[Elasticsearch] Scripting 을 이용한 Field Value 조작하기.

Elastic/Elasticsearch 2015. 8. 17. 14:59

정확하게 조작한다기 보다 저장된 document 의 field value 를 이용한다가 맞을 것 같습니다.

기본 참조 문서는 아래와 같습니다.

https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html


문서를 보셨으면 아시겠지만 기본 groovy 를 사용하게 되어 있습니다.

하지만 보안 이슈로 설정이 기본 false 입니다.

아래와 같이 설정을 변경해 주셔야 사용이 가능 합니다.


[Scripting Enable]

script.inline: on

또는

script.groovy.sandbox.enabled: true

※ 관련 설정에 대한 자세한 내용은 문서를 참고 하시면 됩니다.


아래는 문서 내용 snippet 입니다.


ValueDescription

off

scripting is turned off completely, in the context of the setting being set.

on

scripting is turned on, in the context of the setting being set.

sandbox

scripts may be executed only for languages that are sandboxed

그리고 추후 2.0.0 에서는 위 설정에서 groovy sandbox 는 deprecated 예정이니 참고하세요.

(https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#_groovy_sandboxing)


설정을 해주셨으니 이제 value 에 접근하는 방법에 대해서 알아 보겠습니다.

문서에서는 총 3 가지 방법을 제시해 주고 있습니다.


[Value 접근]

# Document fields

_doc['field_name'].value or values


# Stored fields

_fields['field_name'].value


# Source field

_source['field_name']


※ 여기서 _fields 는 store 옵션을 true 로 하지 않으셨다면 에러가 발생 합니다.


value에 접근 하는 방법을 아셨으니 이 값을 이용한 연산을 어떻게 하는지도 궁금하실 겁니다.

위에서 말씀 드렸듯이 기본적인 language 는 groovy 입니다.

그렇기 때문에 groovy script 를 이용해서 연산 및 처리를 하시면 되겠습니다.


아래는 substring에 대한 커뮤니티의 질문에 대한 예제 코드 입니다.


[script_fields 샘플코드]

GET /test_index/_search

{

    "script_fields": {

        "field1_substring": {

            "script": "_doc['field1'].value"

        }

    }

}


GET /test_index/_search

{

    "script_fields": {

        "field1_substring": {

            "script": "_fields['field1'].value.substring(0, 5)"

        }

    }

}


GET /test_index/_search

{

    "script_fields": {

        "field1_substring": {

            "script": "_source.field1.substring(3, 10)"

        }

    }

}


※ script_fields 를 이용하게 되면 return 시 fields를 통해서 정보가 전달 됩니다. (즉, 필요한 field 가 있다면 추가로 선언해 주셔야 한다는 이야기 입니다.)

※ QueryDSL 을 보시면 아시겠지만 기본 query 구문이 없으면 match_all 로 동작 합니다.

※ 추가적으로 주의 할 점은 field 속성이 not_analyzed 인지 analyzed, store 인지 아닌지 확인 하시고 테스트 하시기 바랍니다.


: