Java对ES的基本操作

更新时间:2023-06-26 15:40:38 阅读: 评论:0

Java对ES的基本操作
1.⾸先介绍⼀下ES的查询类型,SearchType。
丢手绢游戏教案
ES⼀共有四种查询类型:
QUERY_AND_FETCH:主节点将查询请求分发到所有的分⽚中,各个分⽚按照⾃⼰的查询规则即词频⽂档频率进⾏打分排序,然后将结果返回给主节点,主节点对所有数据进⾏汇总排序然后再返回给客户端,此种⽅式只需要和ES交互⼀次。
特点:a.存在数据量和排序问题,主节点会汇总所有分⽚返回的数据,这样数据量会⽐较⼤b.各个分⽚上的规则可能不⼀致
QUERY_THEN_FETCH:主节点将请求分发给所有分⽚,各个分⽚打分排序后将数据的id和分值返回给主节点,主节点收到后进⾏汇总排序,再根据排序后的id到对应的节点读取对应的数据再返回给客户端,此种⽅式需要和ES交互两次
苏州的诗句特点:解决了数据量问题但是排序问题依然存在,是ES的默认查询⽅式
DFS_QUERY_AND_FETCH:和前⾯两种的区别在于将各个分⽚的规则统⼀起来进⾏打分
特点:解决了排序问题,但是仍然存在数据量问题
DFS_QUERY_THEN_FETCH:和前⾯两种的区别在于将各个分⽚的规则统⼀起来进⾏打分,将数据的id和分值返回给主节点,主节点收到后进⾏汇总排序,再根据排序后的id到对应的节点读取对应的数据再返回给客户端
特点:解决了排序和数据量问题但是效率是最差的
2. 分页是会给服务器造成很⼤的压⼒。原因是会将分⽚的数据汇总后数据,如果有N个分⽚就会产⽣N倍的数量。
3.Java操作ES
依赖包:
<dependency>
<groupId>org.elasticarch</groupId>
<artifactId>elasticarch</artifactId>
<version>2.4.1</version>
</dependency>
茶解酒吗import java.io.IOException;
import java.InetAddress;
import java.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import urrent.ExecutionException;
import org.elasticarch.action.bulk.BulkRequestBuilder;
import org.elasticarch.action.bulk.BulkRespon;
import org.elasticarch.index.query.QueryBuilders;
import org.elasticarch.action.delete.DeleteRespon;
import org.GetRespon;
import org.MultiGetItemRespon;
import org.MultiGetRespon;
import org.elasticarch.action.index.IndexRequest;
import org.elasticarch.action.index.IndexRespon;
import org.elasticarch.action.arch.SearchRespon;
import org.elasticarch.action.arch.SearchType;
import org.elasticarch.action.update.UpdateRequest;
import org.elasticarch.action.update.UpdateRespon;
import org.ansport.TransportClient;
import org.de.DiscoveryNode;
import ings.Settings;
import Text;
import ansport.InetSocketTransportAddress;
import ansport.InetSocketTransportAddress;
import nt.XContentBuilder;
import nt.XContentFactory;
import org.elasticarch.arch.SearchHit;
import org.elasticarch.arch.SearchHits;
import org.elasticarch.arch.s.Terms.Bucket;
import org.elasticarch.ics.sum.Sum;
import org.elasticarch.arch.aggregations.AggregationBuilders;
import org.elasticarch.arch.s.Terms;
import org.elasticarch.arch.highlight.HighlightField;
import org.elasticarch.arch.sort.SortOrder;
import com.alibaba.fastjson.JSON;
/**
* Hello world!
*
*/
public class App
{
private static TransportClient client;
static {
System.out.println("初始化链接。。。");
Map<String, String> map = new HashMap<>();
map.put("cluster.name", "my_home");
立志故事Settings.Builder ttings = Settings.builder().put(map);
try {
client = TransportClient.builder().ttings(ttings).build()
.addTransportAddress(new ByName("127.0.0.1"), Integer.parInt("9300")));            List<DiscoveryNode> nodes = tedNodes();
for (DiscoveryNode node : nodes) {
System.out.HostAddress());
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}幼儿园家访内容
}
public static void main(String[] args) throws Exception {
testPreference();
}
/**
* 功能描述: 增加索引
*
* @throws Exception void
* @version 1.0.6
* @author king
*/
public static void addIndex() throws Exception{
System.out.println("============分=====割=====线=============");
XContentBuilder source = createJson4();
/
/ 存json⼊索引中
IndexRespon respon = client.prepareIndex("twitter", "tweet", "4").tSource(source).get();
// 结果获取
String index = Index();
String type = Type();
String type = Type();
String id = Id();
long version = Version();
boolean created = respon.isCreated();
Map<String, Object> result = new HashMap<>();
result.put("index", index);
result.put("type", type);
result.put("id", id);
result.put("version", version);
result.put("created", created);
System.out.JSONString(result));
}
/**
* 功能描述:删除索引
*  void
* @version 1.0.6
* @author king
考试恐惧症
*/
public static void delIndex() {
System.out.println("======================删==除===分===割==线=================");
DeleteRespon deleteRespon = client.prepareDelete("twitter", "tweet", "1").get();
String index = Index();
String type = Type();
String id = Id();
Long version = Version();
Map<String, Object> result = new HashMap<>();
result.put("index", index);
result.put("type", type);
result.put("id", id);
result.put("version", version);
System.out.JSONString(result));
}
/**
* 功能描述: 获取数据
*  void
* @version 1.0.6
* @author king
*/
public static void getData() {
System.out.println("=================获==取==数==据==============");
GetRespon getRespon = client.prepareGet("tom", "cat", "2").execute().actionGet();
String id = Id();
String resultStr = SourceAsString();
System.out.println("id" + id);
System.out.println("resultStr:" + resultStr);
}
/**
*
* */
public static void testGetThread() {
/
/设置线程安全
GetRespon  getrespon = client.prepareGet("tom", "cat", "2").tOperationThreaded(fal).get();        System.out.SourceAsString());
}
}
/**
* 功能描述: 更新ES数据
*
* @throws IOException
* @throws InterruptedException
* @throws ExecutionException void
* @version 1.0.6
* @author king
*/
public static void testUpdate() throws IOException, InterruptedException, ExecutionException {        UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("tom");
updateRequest.id("2");
updateRequest.doc(XContentFactory.jsonBuilder()
.startObject()
.field("phone", "176********")
.field("color", "blue")
.endObject());
UpdateRespon updateRespon = client.update(updateRequest).get();
String index = Index();
String type = Type();
String id = Id();
long version = Version();
Map<String, Object> result = new HashMap<>();
result.put("index", index);
result.put("type", type);
result.put("id", id);
result.put("version", version);
System.out.JSONString(result));
}
/**
* 功能描述: 更新数据
*
* @throws IOException
* @throws InterruptedException
* @throws ExecutionException void
* @version 1.0.6
* @author yaoyaowang
*/
public static void testUpdate1() throws IOException, InterruptedException, ExecutionException {        UpdateRequest updateRequest = new UpdateRequest("tom", "cat", "2");
updateRequest.doc(XContentFactory.jsonBuilder()
.startObject()
.field("phone", "1366688866")
.endObject());
UpdateRespon updateRespon = client.update(updateRequest).get();
String index = Index();
String type = Type();
String id = Id();
long version = Version();
Map<String, Object> result = new HashMap<>();
result.put("index", index);
result.put("type", type);
result.put("id", id);
result.put("version", version);
System.out.JSONString(result));
}
/**
* 功能描述: 新增数据,如果没有插⼊,如果有更新
*
* @throws IOException
* @throws InterruptedException
* @throws ExecutionException void
* @version 1.0.6
* @author yaoyaowang
*/
public static void testUpSert() throws IOException, InterruptedException, ExecutionException {
//查询该数据是否存在,如果不存在则插⼊
IndexRequest indexRequest = new IndexRequest("tom", "cat", "1");
indexRequest.source(XContentFactory.jsonBuilder()
.startObject()
.field("name", "staven")
.field("id", "2")
.field("age", "20")
.
field("x", "F")
.field("address", "America")
.field("phone", "110")
.field("color", "yello"));
//更新该数据,看该数据是否存在,如果不存在,则把上⾯的数据插⼊,如果存在则把数据更新(将"address", "America" 更新为 "address", "America")        UpdateRequest updateRequest = new UpdateRequest("tom", "cat", "1");
updateRequest.doc(XContentFactory.jsonBuilder()五年级下册语文课文
.startObject()
.field("address", "America")
.endObject());
updateRequest.uprt(indexRequest);
UpdateRespon updateRespon = client.update(updateRequest).get();
String index = Index();
String type = Type();
String id = Id();
long version = Version();
Map<String, Object> result = new HashMap<>();
result.put("index", index);
result.put("type", type);
result.put("id", id);
result.put("version", version);
System.out.JSONString(result));
}
/**
* 功能描述: 增加索引
*  void
* @version 1.0.6
* @author king
*/
public static void testMultiGet() {
MultiGetRespon multiGetRespon = client.prepareMultiGet()
.add("tom", "cat", "1")
.add("tom", "cat", "2")
.
五龙河旅游风景区
add("twitter", "tweet", "1")
.get();

本文发布于:2023-06-26 15:40:38,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1055920.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   节点   排序   查询   存在   问题   描述   返回
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图