linus
September 9, 2019, 5:45pm
1
I am trying to set a custom Tokenizer to my index using the Java High Level Rest Client.
I have been looking through the API documentation, but can't find anything aside from the normal REST calls as mentioned in the guide:
https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html
There are guides to updating index settings here:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-indices-put-settings.html
But I can't find any examples on tokenizers. Maybe I have missed a documentation somewhere?
dadoonet
(David Pilato)
September 10, 2019, 11:54am
2
There is not a specific way to generate that. But you can provide a JSON which contains all the index settings, including analyzers like:
{
"settings" : {
"analysis":{
"analyzer":{
"custom_uax_url_email":{
"type":"custom",
"tokenizer" : "uax_url_email",
"filter" : ["standard", "lowercase", "stop"]
},
"francais":{
"type":"custom",
"tokenizer":"standard",
"filter":["lowercase", "stop_francais", "fr_stemmer", "asciifolding", "elision"]
}
},
"filter":{
"stop_francais":{
"type":"stop",
"stopwords":["_french_"]
},
This file has been truncated. show original
Then use the Low Level Client like this:
private static void createIndexWithSettingsInElasticsearch(RestClient client, String index, String settings) throws Exception {
logger.trace("createIndex([{}])", index);
assert client != null;
assert index != null;
Request request = new Request("PUT", "/" + index);
// If there are settings for this index, we use it. If not, using Elasticsearch defaults.
if (settings != null) {
logger.trace("Found settings for index [{}]: [{}]", index, settings);
request.setJsonEntity(settings);
}
Response response = client.performRequest(request);
if (response.getStatusLine().getStatusCode() != 200) {
logger.warn("Could not create index [{}]", index);
throw new Exception("Could not create index ["+index+"].");
}
This file has been truncated. show original
Or use the High Level client like this:
public void createIndex(String index, boolean ignoreErrors, String indexSettings) throws IOException {
logger.debug("create index [{}]", index);
logger.trace("index settings: [{}]", indexSettings);
CreateIndexRequest cir = new CreateIndexRequest(index);
if (!isNullOrEmpty(indexSettings)) {
cir.source(indexSettings, XContentType.JSON);
}
try {
client.indices().create(cir, RequestOptions.DEFAULT);
} catch (ElasticsearchStatusException e) {
if (e.getMessage().contains("resource_already_exists_exception") && !ignoreErrors) {
throw new RuntimeException("index already exists");
}
if (!e.getMessage().contains("resource_already_exists_exception")) {
throw e;
}
}
waitForHealthyIndex(index);
}
HTH
linus
September 10, 2019, 3:14pm
3
Ah, okay. So there's no way to do it directly through the Java API. Will do it the JSON way like you suggested.
Cheers!
dadoonet
(David Pilato)
September 10, 2019, 4:40pm
4
There is a way but not with a nice API. as you saw in docs, you can use something like:
Settings settings =
Settings.builder()
.put("analysis.analyzer.custom_uax_url_email.type", "custom")
.put("analysis.analyzer.custom_uax_url_email.tokenizer", "uax_url_email")
// And so on...
.build();
linus
September 10, 2019, 4:45pm
5
I see. I will give that a try to.
Thank you!
system
(system)
Closed
October 8, 2019, 4:52pm
6
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.