Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Spring factories for elasticsearch
Java

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src
.gitignore
README.textile
pom.xml

README.textile

Spring factories for Elasticsearch

Welcome to the Spring factories for Elasticsearch project

Versions

spring-elasticsearch ElasticSearch
master (0.0.3) 0.19.4
0.0.2 0.19.4
0.0.1 0.19.4

Getting Started

Maven dependency

Import spring-elasticsearch in you project pom.xml file:

<dependency>
  <groupId>fr.pilato.spring</groupId>
  <artifactId>spring-elasticsearch</artifactId>
  <version>0.0.2</version>
</dependency>

Using elasticsearch spring namespace for XML files

In your spring context file, just add namespaces like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:elasticsearch="http://www.pilato.fr/schema/elasticsearch"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.pilato.fr/schema/elasticsearch http://www.pilato.fr/schema/elasticsearch/elasticsearch-0.1.xsd">
</beans>

Define a client Transport bean

In your spring context file, just define a client like this:

<elasticsearch:client id="esClient" />

By default, you will get an Elasticsearch Transport Client connected to an Elasticsearch node already running at localhost:9300

Elasticsearch properties

You can set your elasticsearch properties in a file:

<bean id="esClient"
     class="fr.pilato.spring.elasticsearch.ElasticsearchTransportClientFactoryBean" >
     <property name="settingsFile" value="es.properties" />
</bean>

By default, the factory will use the es.properties file if it exists in your classpath.

Transport Client Network settings

You can (you should) define your nodes settings when using a transport client:

<bean id="esClient"
     class="fr.pilato.spring.elasticsearch.ElasticsearchTransportClientFactoryBean" >
     <property name="esNodes">
       <list>
         <value>localhost:9300</value>
         <value>localhost:9301</value>
       </list>
     </property>
</bean>

Bean properties

Managing indexes and types

If you want to manage indexes and types at startup (creating missing indexes/types and applying mappings):

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="mappings">
		<list>
			<value>twitter/tweet</value>
		</list>
	</property>
</bean>

This will create an Elasticsearch Client that will check
when starting that index twitter exists and tweet type is defined.

If you add in your classpath a file named es/myindex/_settings.json, it will be automatically applied to define
settings for your myindex index.

For example, create the following file src/main/resources/es/twitter/_settings.json in your project:

{
  "index" : {
    "number_of_shards" : 3,
    "number_of_replicas" : 2
  }
}

Also, if you define a file named es/myindex/mytype.json, it will be automatically applied as the mapping for the mytype type in the myindex index.

For example, create the following file src/main/resources/es/twitter/tweet.json in your project:

{
  "tweet" : {
    "properties" : {
      "message" : {"type" : "string", "store" : "yes"}
    }
  }
}

Using convention over configuration

By default, the factory will find every mapping file located under es directory.
So, if you have a mapping file named es/twitter/tweet.json in your classpath, it will be automatically used by the factory
without defining anything:

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" />

You can disable this automatic lookup by setting the autoscan property to false:

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean">
	<property name="autoscan" value="false" />
	<property name="mappings">
		<list>
			<value>twitter/tweet</value>
		</list>
	</property>
</bean>

Creating aliases to indexes

When creating an index, it could be useful to add an alias on it.
For example, if you planned to have indexes per year for twitter feeds (twitter2012, twitter2013, twitter2014) and you want
to define an alias named twitter, you can use the aliases property:

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="aliases">
		<list>
			<value>twitter:twitter2012</value>
			<value>twitter:twitter2013</value>
			<value>twitter:twitter2014</value>
		</list>
	</property>
</bean>

Creating templates

Sometimes it’s useful to define a template mapping that will automatically be applied to new indices created.

For example, if you planned to have indexes per year for twitter feeds (twitter2012, twitter2013, twitter2014) and you want
to define a template named twitter_template, you can use the templates property:

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="templates">
		<list>
			<value>twitter_template</value>
		</list>
	</property>
</bean>

To configure your template you have to define a file named es/_template/twitter_template.json in your project:

{
    "template" : "twee*",
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "tweet" : {
            "properties" : {
                "message" : {
                    "type" : "string",
                    "store" : "yes"
                }
            }
        }
    }
}

Changing classpath search path for mapping and settings files

By default, the factory look in es classpath folder to find if there is index settings or mappings definitions.
If you need to change it, you can use the classpathRoot property:

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="mappings">
		<list>
			<value>twitter/tweet</value>
		</list>
	</property>
	<property name="classpathRoot" value="myownfolder" />
</bean>

So, if a myownfolder/twitter/_settings.xml file exists in your classpath, it will be used by the factory.

Merge mappings

If you need to merge mapping for an existing type, set mergeMapping property to true.

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="mappings">
		<list>
			<value>twitter/tweet</value>
		</list>
	</property>
	<property name="mergeMapping" value="true" />
</bean>

If merging fails, the factory will not start (BeanCreationException
will be raised with a
MergeMappingException cause).

Merge settings

If you need to merge settings for an existing index, set mergeSettings property to true.

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="mappings">
		<list>
			<value>twitter/tweet</value>
		</list>
	</property>
	<property name="mergeSettings" value="true" />
</bean>

If merging fails, the factory will not start.

Force rebuild mappings (use with caution)

For test purpose or for continuous integration, you could force the factory to clean the previous type when starting the client.
It will remove all your datas for that type. Just set forceMapping property to true.

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="mappings">
		<list>
			<value>twitter/tweet</value>
		</list>
	</property>
	<property name="forceMapping" value="true" />
</bean>

Force rebuild templates (use with caution)

For test purpose or for continuous integration, you could force the factory to clean the previous template when starting the client.
Just set forceTemplate property to true.

<bean id="esClient"
    class="fr.pilato.spring.elasticsearch.ElasticsearchClientFactoryBean" >
	<property name="templates">
		<list>
			<value>twitter_template</value>
		</list>
	</property>
	<property name="forceTemplate" value="true" />
</bean>

Thanks

Special thanks to Nicolas Huray for his contribution about templates

Something went wrong with that request. Please try again.