Class EmbeddedElasticsearchPolicy

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    class EmbeddedElasticsearchPolicy
    extends org.junit.rules.ExternalResource
    Used to initialize a single elastic node. For performance reasons (node startup costs), same instance is usually shared across multiple tests.

    This rule should be used as follows:

      public class MyTest {
        @ClassRule
        public static final EmbeddedElasticsearchPolicy RULE = EmbeddedElasticsearchPolicy.create();
    
        @BeforeClass
        public static void setup() {
           // ... populate instance
        }
    
        @Test
        public void myTest() {
          RestClient client = RULE.restClient();
          // ....
        }
      }
      
    See Also:
    ExternalResource
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private org.elasticsearch.client.RestClient client  
      private org.apache.calcite.util.Closer closer  
      private com.fasterxml.jackson.databind.ObjectMapper mapper  
      private EmbeddedElasticsearchNode node  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void after()  
      private static void applyMapping​(com.fasterxml.jackson.databind.node.ObjectNode parent, java.lang.String key, java.lang.String type)
      Creates nested mappings for an index.
      protected void before()  
      static EmbeddedElasticsearchPolicy create()
      Factory method to create this rule.
      (package private) void createIndex​(java.lang.String index, java.util.Map<java.lang.String,​java.lang.String> mapping)
      Creates index in elastic search given a mapping.
      private org.elasticsearch.common.transport.TransportAddress httpAddress()
      HTTP address for rest clients (can be ES native or any other).
      (package private) org.apache.http.HttpHost httpHost()  
      (package private) void insertBulk​(java.lang.String index, java.util.List<com.fasterxml.jackson.databind.node.ObjectNode> documents)  
      (package private) void insertDocument​(java.lang.String index, com.fasterxml.jackson.databind.node.ObjectNode document)  
      (package private) com.fasterxml.jackson.databind.ObjectMapper mapper()
      Exposes Jackson API to be used to parse search results.
      (package private) org.elasticsearch.client.RestClient restClient()
      Low-level http rest client connected to current embedded elastic search instance.
      • Methods inherited from class org.junit.rules.ExternalResource

        apply
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • mapper

        private final com.fasterxml.jackson.databind.ObjectMapper mapper
      • closer

        private final org.apache.calcite.util.Closer closer
      • client

        private org.elasticsearch.client.RestClient client
    • Method Detail

      • before

        protected void before()
                       throws java.lang.Throwable
        Overrides:
        before in class org.junit.rules.ExternalResource
        Throws:
        java.lang.Throwable
      • after

        protected void after()
        Overrides:
        after in class org.junit.rules.ExternalResource
      • create

        public static EmbeddedElasticsearchPolicy create()
        Factory method to create this rule.
        Returns:
        managed resource to be used in unit tests
      • createIndex

        void createIndex​(java.lang.String index,
                         java.util.Map<java.lang.String,​java.lang.String> mapping)
                  throws java.io.IOException
        Creates index in elastic search given a mapping. Mapping can contain nested fields expressed as dots(.).

        Example

          
             b.a: long
             b.b: keyword
          
         
        Parameters:
        index - index of the index
        mapping - field and field type mapping
        Throws:
        java.io.IOException - if there is an error
      • applyMapping

        private static void applyMapping​(com.fasterxml.jackson.databind.node.ObjectNode parent,
                                         java.lang.String key,
                                         java.lang.String type)
        Creates nested mappings for an index. This function is called recursively for each level.
        Parameters:
        parent - current parent
        key - field name
        type - ES mapping type (keyword, long etc.)
      • insertDocument

        void insertDocument​(java.lang.String index,
                            com.fasterxml.jackson.databind.node.ObjectNode document)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • insertBulk

        void insertBulk​(java.lang.String index,
                        java.util.List<com.fasterxml.jackson.databind.node.ObjectNode> documents)
                 throws java.io.IOException
        Throws:
        java.io.IOException
      • mapper

        com.fasterxml.jackson.databind.ObjectMapper mapper()
        Exposes Jackson API to be used to parse search results.
        Returns:
        existing instance of ObjectMapper
      • restClient

        org.elasticsearch.client.RestClient restClient()
        Low-level http rest client connected to current embedded elastic search instance.
        Returns:
        http client connected to ES cluster
      • httpHost

        org.apache.http.HttpHost httpHost()
      • httpAddress

        private org.elasticsearch.common.transport.TransportAddress httpAddress()
        HTTP address for rest clients (can be ES native or any other).
        Returns:
        http address to connect to