Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define custom ElasticSearch Analyzer using Java API

Is there a way to create an index and specify a custom analyzer using the Java API? It supports adding mappings at index creation, but I can't find a way to do something like this without sending the JSON via HTTP PUT:

curl -XPUT localhost:9200/twitter?pretty=true -d '{ "analysis": {        "analyzer": {             "steak" : {                 "type" : "custom",                  "tokenizer" : "standard",                 "filter" : ["snowball", "standard", "lowercase"]             }         }     } }' 

I can build such a query using JSONBuilder, but I can't find no place in the API where to run it, CreateIndexRequest doesn't have anything I can use and neither does client.admin().indices(), as far as I can see. What's the right way to do this?

like image 317
Felix Avatar asked Jun 08 '11 07:06

Felix


People also ask

What is difference between analyzer and Tokenizer in Elasticsearch?

Elasticsearch analyzers and normalizers are used to convert text into tokens that can be searched. Analyzers use a tokenizer to produce one or more tokens per text field. Normalizers use only character filters and token filters to produce a single token.

What is Elasticsearch REST client sniffer?

SniffereditMinimal library that allows to automatically discover nodes from a running Elasticsearch cluster and set them to an existing RestClient instance. It retrieves by default the nodes that belong to the cluster using the Nodes Info api and uses jackson to parse the obtained json response.


2 Answers

You can set analyzer using client.admin().indices().prepareCreate("twitter").setSettings(...). There are several ways to build settings. You can load them from text, map or even use jsonBuilder if that's what you want:

client.admin().indices().prepareCreate("twitter")             .setSettings(Settings.settingsBuilder().loadFromSource(jsonBuilder()                 .startObject()                     .startObject("analysis")                         .startObject("analyzer")                             .startObject("steak")                                 .field("type", "custom")                                 .field("tokenizer", "standard")                                 .field("filter", new String[]{"snowball", "standard", "lowercase"})                             .endObject()                         .endObject()                     .endObject()                 .endObject().string()))             .execute().actionGet(); 
like image 101
imotov Avatar answered Sep 26 '22 23:09

imotov


If you are on a test environnent you can also uses this project which will create your indexes based on Java annotations. https://github.com/tlrx/elasticsearch-test

like image 33
Sebastien Lorber Avatar answered Sep 26 '22 23:09

Sebastien Lorber