ES 첨삭 검사
5750 단어 ES자바elasticsearch
@Autowired
private RestHighLevelClient restHighLevelClient;
//
@Test
public void testCreateIndex() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest("jlr");
IndicesClient indicesClient = restHighLevelClient.indices();
CreateIndexResponse create = indicesClient.create(createIndexRequest, RequestOptions.DEFAULT);
}
//
@Test
public void testExistIndex() throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest("jlr");
IndicesClient indicesClient = restHighLevelClient.indices();
boolean b = indicesClient.exists(getIndexRequest,RequestOptions.DEFAULT);
System.out.println(b);
}
//
@Test
public void testCreateDocument() throws IOException {
User user = new User(" 1",19);
//
IndexRequest indexRequest = new IndexRequest("jlr");
indexRequest.id("2");
indexRequest.timeout("1s");//
// json
indexRequest.source(JSON.toJSONString(user), XContentType.JSON);
//
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.toString());
System.out.println(index.status());
}
//
@Test
public void testGetDocument() throws IOException {
//
GetRequest getRequest = new GetRequest("jlr","1");
//
GetResponse document = restHighLevelClient.get(getRequest,RequestOptions.DEFAULT);
//{"_index":"jlr","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{"age":19,"name":" "}}
System.out.println(document.toString());
// Map{name= , age=19} testCreateDocument
System.out.println(document.getSourceAsMap());
// Key val
Object name = document.getSource().get("name");
System.out.println(name);
}
//
@Test
public void testUpdateDocument() throws IOException {
// Update
UpdateRequest updateRequest = new UpdateRequest("jlr","1");
updateRequest.timeout("1s");// 1s
User user = new User(" ",22);
// POJO json
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
//
UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
System.out.println(update.status());// ok
}
//
@Test
public void testDeleteDocument() throws IOException {
//
DeleteRequest deleteRequest = new DeleteRequest("jlr","11");
deleteRequest.timeout("1s");// 1s
//
DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println(delete.status());// : ok :NOT_FOUND
}
//
@Test
public void testBulkRequest() throws IOException {
//
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout("100s");// 100s
//
List userList = new ArrayList<>();
userList.add(new User("sb1",1));
userList.add(new User("sb2",2));
userList.add(new User("sb3",3));
userList.add(new User("sb4",4));
userList.add(new User("sb5",5));
userList.add(new User("sb6",6));
userList.add(new User("sb7",7));
userList.add(new User("sb8",8));
for (int i=0; i < userList.size(); i++) {
// ADD DeleteRequest ,UpdateRequest
bulkRequest.add(new IndexRequest("jlr")
.id(""+(i+10))// id id UUID
.source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
}
//
BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(!bulk.hasFailures());//bulk.hasFailures false
}
//
//SearchSourceBuilder
//HighlightBuilder
//MatchAllQueryBuilder
//TermQueryBuilder
@Test
public void testSearch() throws IOException {
//
SearchRequest serach = new SearchRequest("jlr");
//
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
HighlightBuilder highlightBuilder= new HighlightBuilder();
//searchSourceBuilder.highlighter()//
// QueryBuilder
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "sb1");// ( , )
//MatchAllQueryBuilder matchAllQueryBuildermatchAllQueryBuilder = QueryBuilders.matchAllQuery();//
//
searchSourceBuilder.query(termQueryBuilder);
serach.source(searchSourceBuilder);
//
SearchResponse search = restHighLevelClient.search(serach, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(search.getHits()));
SearchHit[] hits = search.getHits().getHits();
for(SearchHit item: hits) {
Map sourceAsMap = item.getSourceAsMap();
System.out.println(sourceAsMap);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
_mappings자동으로 인덱스가 생성이 된다. GET books/_mapping DELETE books PUT books/_mapping "type" : "text" text는 역인덱스를 생성하지만 keyword 는 역인덱스를 생...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.