0
点赞
收藏
分享

微信扫一扫

java对api elasticsearch(ES)操作


查看详情
get /person002/_search

查看mappings
get /person002/_mapping

查看所有索引
get _cat/indices

查看100条件记录
GET _search
{
"query": {
"match_all": {}
},
"from": 0,
"size":100

}

查看具体_doc_id

get /person002/_doc/8

创建索引

@Test
public void test2() throws IOException {
//使用client获取操作索引对象
IndicesClient indices = client.indices();
//具体操作获取返回值
//设置名称
CreateIndexRequest person002 = new CreateIndexRequest("person002");
CreateIndexResponse createIndexResponse = indices.create(person002, RequestOptions.DEFAULT);
//返回值判断结果
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println(acknowledged);
}

查询索引

@Test
public void test3() throws IOException {
IndicesClient indices = client.indices();
GetIndexRequest indexRequest = new GetIndexRequest("person002");
GetIndexResponse response = indices.get(indexRequest, RequestOptions.DEFAULT);
Map<String, MappingMetaData> mappings = response.getMappings();
for (String key : mappings.keySet()) {
System.out.println(key + "=====" + mappings.get(key).getSourceAsMap());
}
}

添加文档,使用对象作为数据

@Test
public void test4() throws IOException {
Shop shop = new Shop();
shop.setId(223L);
shop.setName("dong");
shop.setAddress("广东省汕头大学");
String toJSONString = JSON.toJSONString(shop);
IndexRequest indexRequest = new IndexRequest("person002")
.id(String.valueOf(shop.getId())).source(toJSONString, XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(index));
}

根据id查询文档

@Test
public void test5() throws IOException {
GetRequest indexRequest = new GetRequest("person002", "223");
GetResponse response = client.get(indexRequest, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
System.out.println("==============================");
System.out.println(JSON.toJSONString(response));;
}

elasticsearch批量操作和导入

/**

* @author: samxie

* @create: 2022/6/10

* @Description:

* @FileName: ElasticsearchTest

* @History:

* @自定义内容:

*/

@RunWith(SpringRunner.class)

@SpringBootTest

public class ElasticsearchTest {

@Resource

private RestHighLevelClient client;





private final String INDEX = "person002";



//批量操作

@Test

public void test8() throws IOException {

BulkRequest bulkRequest = new BulkRequest();



DeleteRequest deleteRequest = new DeleteRequest(INDEX, "5");

bulkRequest.add(deleteRequest);



Map<String, Object> map = new TreeMap<>();

map.put("name", "六号");

IndexRequest indexRequest = new IndexRequest(INDEX).id("6").source(map);

bulkRequest.add(indexRequest);



Map<String, Object> mapUpdate = new HashMap<>();

mapUpdate.put("name", "三号54333333333333333333333333");

Up dateRequest updateRequest = new UpdateRequest(INDEX, "2").doc(mapUpdate);

bulkRequest.add(updateRequest);



BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);

System.out.println(response.status());

System.out.println("=====" + JSON.toJSONString(response));

}



//导入

@Data

class Goods {

private String id = UUID.randomUUID().toString();

private String name;

private String specStr;

private Map spec;



public Goods(String name, String specStr, Map spec) {

this.name = name;

this.specStr = specStr;

this.spec = spec;

}

}



@Test

public void test9() throws IOException {

List<Goods> goodsList = new Vector<>();

goodsList.add(new Goods("1", null, null));

goodsList.add(new Goods("2", null, null));

goodsList.add(new Goods("2dw", null, null));

goodsList.add(new Goods("2wwd", null, null));



//bulk import

BulkRequest bulkRequest = new BulkRequest();

for (Goods goods : goodsList) {

String specStr = goods.getSpecStr();

Map map = JSON.parseObject(specStr, Map.class);

goods.setSpec(map);

String toJSONString = JSON.toJSONString(goods);

IndexRequest indexRequest = new IndexRequest(INDEX).source(toJSONString, XContentType.JSON);

System.out.println("=======" + JSON.toJSONString(indexRequest));

bulkRequest.add(indexRequest);

}

System.out.println("----" + bulkRequest);

BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT);

System.out.println(response.status());



}



@Test

public void test3() throws IOException {

SearchRequest searchRequest = new SearchRequest(INDEX);



SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();



QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();

sourceBuilder.query(queryBuilder);



searchRequest.source(sourceBuilder);



SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);



SearchHits hits = searchResponse.getHits();



long total = hits.getTotalHits().value;

System.out.println("总数:" + total);



SearchHit[] hits1 = hits.getHits();

for (SearchHit searchHit : hits1) {

String sourceAsString = searchHit.getSourceAsString();

System.out.println("=====" + sourceAsString);

}









}



//类型:text:会分词;keyword:不会分词,将全部内容作为一个词条,支持聚合





}


举报

相关推荐

0 条评论