使用Java API定义自定义ElasticSearch Analyzer [英] Define custom ElasticSearch Analyzer using Java API
问题描述
有没有办法创建索引并使用Java API指定自定义分析器?它支持在索引创建时添加映射,但是我无法找到一种方法来执行类似的操作,而不需要通过HTTP PUT发送JSON:
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"]
}
}
}
}'
我可以使用JSONBuilder构建这样一个查询,但是我在API中找不到任何地方可以运行它, CreateIndexRequest
没有任何可以使用的东西,而且$ code> client.admin ().indices(),尽可能看到。
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?
推荐答案
您可以使用 client.admin()设置分析器。指数()。prepareCreate( 推)。setSettings(...)
。有几种方法来构建设置。您可以从文本,地图或甚至使用jsonBuilder加载它们,如果这是您想要的:
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();
这篇关于使用Java API定义自定义ElasticSearch Analyzer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!