elasticsearch7.16.2官方下载地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-16-2
kibana7.16.2官方下载地址:https://www.elastic.co/cn/downloads/past-releases/kibana-7-16-2
1.windows系统选择下载windows-x86_64.zip后解压,先双击bin\elasticsearch.bat运行elasticsearch
2.等待访问localhost:9200成功后,再双击bin\kibana.bat运行kibana,访问localhost:5601查看是否运行成功。
1.指定jakarta.json为最新版本
<properties>
<java.version>1.8</java.version>
<!-- 如果报错:java.lang.NoClassDefFoundError: jakarta/json/JsonException,是因为Java Api Client依赖1.1.6版本的jakarta.json,指定版本为2.0.1即可-->
<jakarta-json.version>2.0.1</jakarta-json.version>
</properties>
2.加入Java API Client (Elasticsearch)和jackson (JSON)依赖
目前,最新版本为4.3.2的Spring Data Elasticsearch (SpringBoot2.6.2通过spring-data 9300端口TCP方式操作Elasticsearch)只支持到Elasticsearch7.15.2版本为止,elasticsearch-rest-high-level-client也已经在7.15.0弃用,因此7.16及以上版本可以改为使用Java API Client。
<!-- Elasticsearch Java API Client
https://mvnrepository.com/artifact/co.elastic.clients/elasticsearch-java -->
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>7.16.2</version>
</dependency>
<!-- 为Elasticsearch引入的Json依赖
https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
编写了创建单个索引,创建单个文档,批处理创建多个文档,查询索引,按条件过滤范围查询等方法,测试成功。
package com.zhy.springboot;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.indices.CreateIndexRequest;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Zhu HongYi
* @version 1.0
* @Date: 2022/03/07/21:37
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootLearnApplication.class)
public class ElasticSearchTest {
static JacksonJsonpMapper jacksonJsonpMapper=new JacksonJsonpMapper();//json映射器
ElasticsearchTransport transport;
ElasticsearchClient client;
String index="products";
@Before
public void startup(){
RestClient restClient=RestClient.builder(new HttpHost("localhost",9200)).build();
transport=new RestClientTransport(restClient,jacksonJsonpMapper);//创建传输层
client=new ElasticsearchClient(transport);
}
@After
public void after() throws IOException {
transport.close();
System.out.println("关闭传输层成功");
}
@Test
public void createIndex() throws IOException {//创建索引
CreateIndexResponse createIndexResponse=
client.indices().create(new CreateIndexRequest.Builder().index(index).build());
// client.indices().create(c->c.index(index));//lambda写法
System.out.println("创建索引操作测试:"+createIndexResponse.acknowledged());
}
@Test
public void createDocument() throws IOException {//创建单个文档(map类型) request [PUT http://localhost:9200/products/_doc/1/_create]
Map<String,String> map=new HashMap<>();
map.put("name","product 0");
map.put("price",String.valueOf(50+(int)(Math.random()*500)));
CreateResponse createResponse=
client.create(CreateRequest.of(c->c.index(index).id("1").type("_doc").document(map)));//id已存在则会创建失败
System.out.println("创建单个文档测试:"+createResponse.result().jsonValue());
}
@Test
public void bulkCreateDocuments() throws IOException {//批处理创建多个文档
List<BulkOperation> list=new ArrayList<>();
for(int i=10;i<20;i++){
Map<String,String> map=new HashMap<>();
map.put("name","product "+i);
map.put("price",String.valueOf(50+(int)(Math.random()*500)));
int id=i;
list.add(BulkOperation.of(c->c.create(e->e.index(index).id(String.valueOf(id)).document(map))));
}
BulkResponse bulkResponse=
client.bulk(new BulkRequest.Builder().index(index).operations(list).build());//.type("_doc")加不加都可以
System.out.println("批量创建多个文档测试:(例:查看第一个文档创建结果)"+bulkResponse.items().get(0).result());//created
}
@Test
public void searchIndex() throws IOException {//查询索引 request [POST http://localhost:9200/products/_search?typed_keys=true]
SearchResponse<Object> searchResponse=client.search(new SearchRequest.Builder().index(index).build(),Object.class);
List<Hit<Object>> list=searchResponse.hits().hits();
List<Object> result=new ArrayList<>();
for(Hit<Object> hit:list){
result.add(hit.source());
}
System.out.println(result);//[{price=206, name=product 0}, {price=548, name=product 5},...]
}
@Test
public void searchInRangeOfPrice() throws IOException {//从products索引中搜索价格字段值>=300&&<=500的文档
SearchResponse<Object> searchResponse=
client.search(
s->s.index(index).query(
c->c.bool(
b->b.filter(
q->q.range(
v->v.field("price").gte(JsonData.of("300")).lte(JsonData.of("500"))))))
,Object.class);
List<Hit<Object>> list=searchResponse.hits().hits();
List<Object> result=new ArrayList<>();
for(Hit<Object> hit:list){
result.add(hit.source());
}
System.out.println(result);//[{price=396, name=product 6}, {price=483, name=product 8}, {price=393, name=product 9}, ...]
}
@Test
public void deleteDocument() throws IOException {//删除 request [DELETE http://localhost:9200/products/_doc/1]
System.out.println("查看删除是否成功:"+client.delete(d->d.index(index).id("1")).result().jsonValue());//deleted
}
}
1.可以通过Get请求查询索引内容localhost:9200/products/
2.还可以通过Kibana创建索引模式匹配想要查看的索引,从而提供对索引的可视化管理。
http://localhost:5601/app/management/kibana/indexPatterns
点击Management-Stack Management-Index patterns-Create index pattern输入自己创建的索引名称(比如products),即可在index pattern处查看索引的字段,在Analytics-Discover里
选择已创建的索引模式查看其中的文档。
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
从MB升级到新的MBP后,Apple的迁移助手没有移动我的gem。我这次是通过macports安装rubygems,希望在下次升级时避免这种情况。有什么我应该注意的陷阱吗? 最佳答案 如果你想把你的gems安装在你的主目录中(在传输过程中应该复制过来,作为一个附带的好处,会让你以你自己的身份运行geminstall,而不是root),将gemhome:键设置为您在~/.gemrc中的主目录中的路径. 关于通过MacPorts的RubyGems是个好主意吗?,我们在StackOverf
当我执行>rvminstall1.9.2时一切顺利。然后我做>rvmuse1.9.2也很顺利。但是当涉及到ruby-v时..sam@sjones:~$rvminstall1.9.2/home/sam/.rvm/rubies/ruby-1.9.2-p136,thismaytakeawhiledependingonyourcpu(s)...ruby-1.9.2-p136-#fetchingruby-1.9.2-p136-#downloadingruby-1.9.2-p136,thismaytakeawhiledependingonyourconnection...%Total%Rece
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
如何检查Ruby文件是否是通过“require”或“load”导入的,而不是简单地从命令行执行的?例如:foo.rb的内容:puts"Hello"bar.rb的内容require'foo'输出:$./foo.rbHello$./bar.rbHello基本上,我想调用bar.rb以不执行puts调用。 最佳答案 将foo.rb改为:if__FILE__==$0puts"Hello"end检查__FILE__-当前ruby文件的名称-与$0-正在运行的脚本的名称。 关于ruby-检查是否