管理索引是客户端应用常用的一些动作,比如我们创建,删除,打开 及关闭索引等操作。在今天的文章中,我将描述如何在 Java 客户端应用中对索引进行管理。
我们需要阅读之前的文章 “Elasticsearch:在 Java 客户端中使用 truststore 来创建 HTTPS 连接”。在那篇文章中,我们详述了如何在 Java 客户端应用中和 Elasticsearch 建立连接。在这里就不再累述了。
为了方便大家的阅读,我创建了如下的一个 github 仓库:GitHub - liu-xiao-guo/elasticsearchjava-manage-index
在代码中我创建了如下的一个 class:
IndexOperations.java
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.json.JsonpMapper;
import jakarta.json.Json;
import jakarta.json.stream.JsonParser;
import java.io.IOException;
import java.io.StringReader;
public class IndexOperations {
private final ElasticsearchClient client;
public IndexOperations(ElasticsearchClient client)
{ this.client = client; }
// Check whether an index exists or not
public boolean checkIndexExists(String name) throws IOException {
return client.indices().exists(c -> c.index(name)).value();
}
// Create an index if it does not exist
public void createIndex(String name) throws IOException {
client.indices().create(c -> c.index(name));
}
// Delete an index if it exists
public void deleteIndex(String name) throws IOException {
client.indices().delete(c -> c.index(name));
}
// Close an index
public void closeIndex(String name) throws IOException {
client.indices().close(c -> c.index(name));
}
// Open an index
public void openIndex(String name) throws IOException {
client.indices().open(c -> c.index(name));
}
// Create an index with mappings defined
public void putMapping(String index, String mappings) throws IOException {
JsonpMapper mapper = client._transport().jsonpMapper();
JsonParser parser = Json.createParser(new StringReader(mappings));
CreateIndexRequest request_create = new CreateIndexRequest.Builder()
.index(index)
.mappings(TypeMapping._DESERIALIZER.deserialize(parser, mapper))
.build();
CreateIndexResponse response_create = client.indices().create(request_create);
}
}
通过这个 class 的使用,我们可以对一个索引进行 create,delete,open,close 及检查是否存在。关于使用 mapping 来创建索引,更多的信息可以参考文章 “Elasticsearch:在 Java 应用中创建 mappings,批量写入及更新 - Java client 8.x”。
在代码中,我们需要修改相应的部分创建 ElasticsearchClient 实例。我们使用如下的代码来示例化 IndexOperations:
ElasticsearchJava.java
try {
makeConnection_truststore();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
IndexOperations io = new IndexOperations(client);
我们可以使用如下的代码来对索引的操作进行测试:
io.createIndex(INDEX_NAME);
Thread.sleep(1000);
io.closeIndex(INDEX_NAME);
io.openIndex(INDEX_NAME);
io.deleteIndex(INDEX_NAME);
String mappings = "{\n" +
" \"properties\" : {\n" +
" \"id\" : {\n" +
" \"type\" : \"keyword\" \n" +
" },\n"+
" \"name\" : {\n" +
" \"type\" : \"text\",\n" +
" \"fields\" : {\n" +
" \"keyword\" : {\n" +
" \"type\" : \"keyword\",\n" +
" \"ignore_above\" : 256 \n" +
" }\n" +
" } \n" +
" }, \n" +
" \"price\" : { \n" +
" \"type\" : \"long\" \n" +
" } \n" +
" }\n" +
"}\n";
io.putMapping("test1", mappings);
我们在代码中放置了 1 秒的延迟(Thread.wait(1000)) 以防止对索引的快速操作,因为它们的分片分配是异步的,并且它们需要几毫秒才能准备好。 最佳实践是不使用类似的 hack,而是在执行进一步操作之前轮询索引的状态,并且仅在它变为绿色时执行这些操作。
在上面的代码的最后部分,我们使用 io 来创建一个具有 mappings 的索引。执行完后,我们可以使用如下的命令来查看:
GET test1/_mapping
上面的命令显示:
{
"test1": {
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"price": {
"type": "long"
}
}
}
}
} 对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht