# Elasticsearch: Creating template using java

**URL:** <https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456>\
**Category:** Elasticsearch\
**Created:** [February 11, 2016, 6:34am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456 "2016-02-11T06:34:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![krushnat\_khawale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krushnat_khawale/32/6652_2.png) [@krushnat\_khawale](https://discuss.elastic.co/u/krushnat_khawale)\
**Post date:** [February 11, 2016, 6:34am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/1 "2016-02-11T06:34:49Z")

</div>

**I want to create a custom template in elasticsearch as it by default analyses all fields.**  
**I want only specific fields to be analysed and I want to do this using a java client.**

**Also where can I find required jar for the same?**

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 11, 2016, 7:03am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/2 "2016-02-11T07:03:59Z")

</div>

Maven central

---

<div class="post-metadata">

**Author:** ![krushnat\_khawale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krushnat_khawale/32/6652_2.png) [@krushnat\_khawale](https://discuss.elastic.co/u/krushnat_khawale)\
**Post date:** [February 11, 2016, 7:10am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/3 "2016-02-11T07:10:08Z")

</div>

@dadoonet Thanks for the sooner reply.  
Yeah visited maven central, Also downloaded it.

I am using ES 2.1.0 and the jar which is available there for 2.1.0 does not have the IndicesAdminClient  
class also its just one jar.

So I've downloaded downloaded ES 2.2.0 which constist of number of jars there.  
My question is,  
will it work for 2.1.0?  
and the second question is, are all jars are required to import?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 11, 2016, 7:28am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/4 "2016-02-11T07:28:49Z")

</div>

Not sure about which jars you are referring to.

Here are the jars you basically need (2.1 branch):

```auto
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Elasticsearch: Core 2.1.3-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ elasticsearch ---
[INFO] org.elasticsearch:elasticsearch:jar:2.1.3-SNAPSHOT
[INFO] +- org.hamcrest:hamcrest-all:jar:1.3:test
[INFO] +- com.carrotsearch.randomizedtesting:randomizedtesting-runner:jar:2.1.16:test
[INFO] | \- junit:junit:jar:4.11:test
[INFO] +- org.apache.lucene:lucene-test-framework:jar:5.3.1:test
[INFO] | +- org.apache.lucene:lucene-codecs:jar:5.3.1:test
[INFO] | \- org.apache.ant:ant:jar:1.8.2:test
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.3.6:test
[INFO] | +- org.apache.httpcomponents:httpcore:jar:4.3.3:test
[INFO] | +- commons-logging:commons-logging:jar:1.1.3:test
[INFO] | \- commons-codec:commons-codec:jar:1.6:test
[INFO] +- com.google.jimfs:jimfs:jar:1.0:test
[INFO] +- org.apache.lucene:lucene-core:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-backward-codecs:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-analyzers-common:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-queries:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-memory:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-highlighter:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-queryparser:jar:5.3.1:compile
[INFO] | \- org.apache.lucene:lucene-sandbox:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-suggest:jar:5.3.1:compile
[INFO] | \- org.apache.lucene:lucene-misc:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-join:jar:5.3.1:compile
[INFO] | \- org.apache.lucene:lucene-grouping:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-spatial:jar:5.3.1:compile
[INFO] | \- org.apache.lucene:lucene-spatial3d:jar:5.3.1:compile
[INFO] +- org.apache.lucene:lucene-expressions:jar:5.3.1:compile
[INFO] | +- org.antlr:antlr-runtime:jar:3.5:compile
[INFO] | +- org.ow2.asm:asm:jar:4.1:compile
[INFO] | \- org.ow2.asm:asm-commons:jar:4.1:compile
[INFO] +- com.spatial4j:spatial4j:jar:0.5:compile
[INFO] +- com.vividsolutions:jts:jar:1.13:compile
[INFO] +- com.github.spullara.mustache.java:compiler:jar:0.8.13:compile
[INFO] +- com.google.guava:guava:jar:18.0:compile
[INFO] +- com.carrotsearch:hppc:jar:0.7.1:compile
[INFO] +- joda-time:joda-time:jar:2.8.2:compile
[INFO] +- org.joda:joda-convert:jar:1.2:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.6.2:compile
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.6.2:compile
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.6.2:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.15:compile
[INFO] +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.6.2:compile
[INFO] +- io.netty:netty:jar:3.10.5.Final:compile
[INFO] +- com.ning:compress-lzf:jar:1.0.2:compile
[INFO] +- com.tdunning:t-digest:jar:3.0:compile
[INFO] +- org.hdrhistogram:HdrHistogram:jar:2.1.6:compile
[INFO] +- commons-cli:commons-cli:jar:1.3.1:compile
[INFO] +- org.codehaus.groovy:groovy-all:jar:indy:2.4.4:compile
[INFO] +- log4j:log4j:jar:1.2.17:compile
[INFO] +- log4j:apache-log4j-extras:jar:1.2.17:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.6.2:compile
[INFO] +- net.java.dev.jna:jna:jar:4.1.0:compile
[INFO] \- com.twitter:jsr166e:jar:1.1.0:compile

```

---

<div class="post-metadata">

**Author:** ![krushnat\_khawale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krushnat_khawale/32/6652_2.png) [@krushnat\_khawale](https://discuss.elastic.co/u/krushnat_khawale)\
**Post date:** [February 11, 2016, 7:34am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/5 "2016-02-11T07:34:36Z")

</div>

I've downloaded highlighted(RED) jar from MavenCentral, and there is only one jar with four classes.  
But ES 2.2.0 jar contains all the jars you've listed above.

 ![](https://us1.discourse-cdn.com/elastic/original/2X/f/f10005ab8e8925a84684f19d206ed246c46b45bd.png)

---

<div class="post-metadata">

**Author:** ![krushnat\_khawale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krushnat_khawale/32/6652_2.png) [@krushnat\_khawale](https://discuss.elastic.co/u/krushnat_khawale)\
**Post date:** [February 11, 2016, 7:38am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/6 "2016-02-11T07:38:55Z")

</div>

Also after googling a lot, I haven't found a single program that demonstrates using API completely.  
So do you have any link of something which I can refer in my application, because I have gone through  
documentation of API and it simple confuses, because snippest are explained, where a complete program will be useful.

---

<div class="post-metadata">

**Author:** ![krushnat\_khawale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krushnat_khawale/32/6652_2.png) [@krushnat\_khawale](https://discuss.elastic.co/u/krushnat_khawale)\
**Post date:** [February 11, 2016, 7:48am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/7 "2016-02-11T07:48:21Z")

</div>

May I request you to share the link to download... the above referenced jars.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 11, 2016, 8:03am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/8 "2016-02-11T08:03:07Z")

</div>

No sorry. That's too much work. You have to search that by yourself.

That's the reason using a dependency management system like Maven or Gradle is highly recommended.  
Because it's then just a few lines of xml code:

```auto
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.1.1</version>
</dependency>

```

About the Java guide, have a look here: [https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html)

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 11, 2016, 8:17am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/9 "2016-02-11T08:17:33Z")

</div>

Listen to David, it is far easier to use Maven or Gradle nowadays. You can  
view the dependencies in the Maven repositories:

[http://mvnrepository.com/artifact/org.elasticsearch/elasticsearch/2.2.0](http://mvnrepository.com/artifact/org.elasticsearch/elasticsearch/2.2.0)

Not every jar is needed under most circumstances for the client, but it is  
best to have them.

Ivan

---

<div class="post-metadata">

**Author:** ![krushnat\_khawale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/krushnat_khawale/32/6652_2.png) [@krushnat\_khawale](https://discuss.elastic.co/u/krushnat_khawale)\
**Post date:** [February 11, 2016, 8:49am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/10 "2016-02-11T08:49:01Z")

</div>

Thanks @Ivan and @dadoonet for helping out in this.

We are facing issue in searching data in indices. When I search in kibana for with multiword text or apply filter for a perticular column, it simple creates two different bars for each word in search text.  
For example, data is  
Google Inc. – 15 messages  
Facebook Inc. – 25 messages  
Linkedin Inc. – 33 messages

When we query the index for ASN fields, you are going to get 15 hits for Google, 25 hits for Facebook, 33 hits for Linkedin and 73 hits for Inc  
And after googling we found that, by default it analyses all fields which we don't want.

As we are creating a solution along with ELK, and we have to demonstrate it by ASAP(tomorrow).  
**Going through API, to just create index with a template taking too much time and making us trial and error for each class and methods.**

Can I expect a very simple java sample for creating an index with template?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 11, 2016, 9:03am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/11 "2016-02-11T09:03:37Z")

</div>

Sorry but is that the same question?

You have a demo to build with ELK?

Have a look at [http://david.pilato.fr/blog/2015/11/17/index-twitter-on-found/](http://david.pilato.fr/blog/2015/11/17/index-twitter-on-found/)

Why do you want to create a Java App for this?

That being said, I wrote some demo code there: [https://github.com/dadoonet/legacy-search](https://github.com/dadoonet/legacy-search)

Might help.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [February 11, 2016, 9:10am UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/12 "2016-02-11T09:10:41Z")

</div>

I would use templates, as well, templates. 🙂 You can define templates  
beforehand and have them match any index whose name matches a pattern. You  
can define the templates in JSON and just PUT the template via curl.

It is two steps, but I personally prefer to have the templates defined in  
JSON so that I can have them in source control. There used to be file-based  
templates, but those were removed (grrrr).

Ivan

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:17pm UTC](https://discuss.elastic.co/t/elasticsearch-creating-template-using-java/41456/13 "2017-07-05T23:17:16Z")

</div>


