在pom.xml中添加依赖:
成都创新互联公司专注于企业营销型网站建设、网站重做改版、乐安网站定制设计、自适应品牌网站建设、H5开发、商城建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为乐安等各大城市提供网站开发制作服务。
<es.version>6.1.1</es.version>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${es.version}</version>
</dependency>
然后创建一个单元测试类ESApp:
private TransportClient client;
@Before
public void setUp() throws Exception {
Settings settings = Settings.builder()
.put("cluster.name", "mycluster")
.put("client.transport.sniff", "true")//增加自动嗅探配置
.build();
client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new TransportAddress(InetAddress.getByName("10.8.24.94"), 9300));
System.out.println(client.toString());
}
运行后报错
java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
运行后成功拿到ES的client:
@Test
public void createIndex() {
client.admin().indices().prepareCreate(INDEX).get();
System.out.println("创建Index成功");
}
@Test
public void deleteIndex() {
client.admin().indices().prepareDelete(INDEX).get();
System.out.println("删除Index成功");
}
//不推荐使用,太繁琐拼json格式
@Test
public void createDoc() {
String json = "{\"name\":\"若泽数据\"}";
IndexResponse response = client.prepareIndex(INDEX, TYPE, "100")
.setSource(json, XContentType.JSON)
.get();
}
//推荐使用
@Test
public void test01() throws Exception {
Map<String, Object> json = new HashMap<String, Object>();
json.put("name", "ruozedata");
json.put("message", "trying out Elasticsearch");
IndexResponse response = client.prepareIndex(INDEX, TYPE, "101").setSource(json).get();
System.out.println(response.getVersion());
}
//推荐使用
@Test
public void test02() throws Exception {
XContentBuilder builder = jsonBuilder()
.startObject()
.field("user", "ruoze")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject();
IndexResponse response = client.prepareIndex(INDEX, TYPE, "102").setSource(builder).get();
System.out.println(response.getVersion());
}
@Test
public void getDoc() {
GetResponse response = client.prepareGet(INDEX, TYPE, "100").get();
System.out.println(response.getSourceAsString());
}
@Test
public void getDocsByIds() {
MultiGetResponse responses = client.prepareMultiGet()
.add(INDEX, TYPE,"100")
.add(INDEX, TYPE, "101", "102", "1000")
.get();
for (MultiGetItemResponse response : responses) {
GetResponse res = response.getResponse();
if (res.isExists()) {
System.out.println(res);
} else {
System.out.println("没有这条数据");
}
}
}
当前标题:Elasticsearch常用操作解析
标题网址:https://www.cdcxhl.com/article0/poeiio.html
成都网站建设公司_创新互联,为您提供ChatGPT、品牌网站制作、动态网站、网站营销、网站排名、面包屑导航
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联