elasticsearch6.4.3 연결 서버 작업 삭제 및 전체 텍스트 검색

8037 단어
여기는springboot을 사용해서pom 파일 org를 가져옵니다.elasticsearch elasticsearch org.springframework.boot spring-boot-starter-data-elasticsearch
여기서 기본적으로 식별되는elasticsearch의 최신 버전은 6.4.3입니다.
먼저 kibana에 인덱스를 만들고 mapping 맵 PUT/tgdsm {'settings': {'number_of_shards': 3,'number_of_replicas': 0},'mappings': {'properties': {'documentid': {'type':'long'},'documentname': {'type':'text','analyzer': ik_max_word'},'documentsis ze: {"type":"long"}, “uploader”:{ “type”:“text”, “analyzer”:“ik_max_word” }, “uploadtime”:{ “type”:“date” } } } } }
10개의 데이터를 삽입하여 PUT/tgdsm/document/1 {'documentid': 1,'documentname':'산협 수리 댐 파일','documentsize': 1056,'uploader':'유비','uploadtime':'2019-01-16'}PUT/tgdsm/document/2 {'documentid': 2,'documentname':'갈주댐 수리 시스템','documentsize': 1059,'uploader """고강도""uploadtime"":"2019-03-15""}PUT/tgdsm/document/3 {"documentid": 3, "documentname": "운력저수지 수리 파일", "documentsize": 2056, "uploader": "하계명", "uploadtime": "2018-01-16"} PUT/tgdsm/document/4 {"documentid": 4, "documentname": "온주건화수리시스템", "documentsize": 2099, "uploader": 명","uploadtime":"2018-06-16"} PUT/tgdsm/document/5{"documentid": 5, "documentname": "댐 안전자료", "documentsize": 20556, "uploader": "유비", "uploadtime": "2018-02-16"} PUT/tgdsm/document/6 {"documentid": 6, "documentname": "감리온도제어 예시 파일", "documentsize": 2996, "uploader": "하계명", "uploadtime": "2018-08"} PUT/tgdsm/document/7 {"documentid": 7,"documentname": "복주댐 방비", "documentsize": 256, "uploader": "고강","uploadtime": "2018-01-16"} PUT/tgdsm/document/8 {"documentid": 8, "documentname": 소주댐 조작","documentsize": 16,"uploader":"하계명","uploadtime":"2018-01-16"} PUT/tgdsm/document/9 {"documentid": 9,"documentname":"제남이합수","documentsize": 2996,"uploader":"장형","uploadtime":"2018-01-17"}PUT/tgdsm/document/10 {"documentid": 10, "documentname": "십언 안전 방비 자료", "documentsize": 2089, "uploader": "유비", "uploadtime": "2019-01-16"}
주니트 테스트 하기1.(id별로 조회) @Test public void esSearchTest () throws Exception {Settings settings=Settings.builder ().put ("cluster.name", "my-application").build (); Transport Client = New PreBuiltTransport Client (settings).addTransport Adress (InetAdress.getByName (192.168.109), 9300)), GetResponse response=client.prepareGet("index1", "blog", "10").execute().actionGet(); System.out.println(response.getSourceAsString()); client.close(); }
2 (추가 작업)/* * document 추가 * @throws Exception */@Test public void esAdddTest () throws Exception {Settings settings=Settings.builder ().put ("cluster.name", "my-application").build (); Transport Client = New PreBuiltTransport Client (settings).addtransport Adress (InetAdress.get. ByName("192.168.1.109"), 9300);
	XContentBuilder doc=XContentFactory.jsonBuilder()
			.startObject()
			.field("id", "1")
			.field("title", "Java ")
			.field("content", " , 。")
			.field("postdate", "2018-05-20")
			.field("url", "csdn.net/79239072")
			.endObject();
	IndexResponse response=client.prepareIndex("index1", "blog", "10").setSource(doc).get();
	System.out.println("============="+response.status());
		}
3,( )
/**
 *  Id document
 * @throws UnknownHostException 
 */
@Test
public void esDel() throws UnknownHostException {
	Settings settings=Settings.builder().put("cluster.name", "my-application").build();
	TransportClient client=new PreBuiltTransportClient(settings)
			.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.109"), 9300));
    
	DeleteResponse response=client.prepareDelete("index1", "blog", "10").get();
    System.out.println("------------"+response.status());
}
 
4
/**
 *  multi
 * @throws UnknownHostException 
 */
 // 
@Test
public void essearchAll1() throws UnknownHostException {
	Settings settings=Settings.builder().put("cluster.name", "my-application").build();
	TransportClient client=new PreBuiltTransportClient(settings)
			.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.109"), 9300));
    QueryBuilder builder=QueryBuilders.multiMatchQuery(" ","documentname","uploader");
    SearchResponse response=client.prepareSearch("tgdsm")
    		.setTypes("document")
    		.setQuery(builder)

//.setSize(3) .get();/* System.out.println("결과"+response);*/SearchHits hits = response.getHits(); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); Map map = hit.getSourceAsMap(); for(String key:map.keySet()) {/System.out.println(key+"="+map.get(key));/} }
}

//두 번째 @Test public void essearchAll2 () throws UnknownHostException {Settings settings=Settings.builder ().put ("cluster.name", "my-application").build (); TransportClient = New PreBuiltTransportClient (settings).addTransportAdress (InetAdress.getByName ("192.168.109"), 9300);QueryBuilder builder=QueryBuilders.multiMatchQuery("댐 수리",“documentname”,“uploader”); SearchResponse response=client.prepareSearch(“tgdsm”) .setTypes(“document”) .setQuery(builder)//.setSize(3) .get();/* System.out.println("결과"+response);*/SearchHits hits = response.getHits(); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); Map map = hit.getSourceAsMap(); for(String key:map.keySet()) {/System.out.println(key+"="+map.get(key));/} }
}

//세 번째 @Test public void essearchAll3 () throws UnknownHostException {Settings settings=Settings.builder ().put ("cluster.name", "my-application").build (); TransportClient = New PreBuiltTransportClient (settings).addTransportAdress (InetAdress.getByName ("192.168.109"),9300);QueryBuilder builder=QueryBuilders.multiMatchQuery("댐 수리 강도",“documentname”,“uploader”); SearchResponse response=client.prepareSearch(“tgdsm”) .setTypes(“document”) .setQuery(builder)//.setSize(3) .get();/* System.out.println("결과"+response);*/SearchHits hits = response.getHits(); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); Map map = hit.getSourceAsMap(); for(String key:map.keySet()) {/System.out.println(key+"="+map.get(key));/} }
}

//네 번째는 페이지 나누기 상황/**/@Test public void essearchAll4 () throws UnknownHostException {Settings settings=Settings.builder ().put ("cluster.name", "my-application").build (); TransportClient = New PreBuiltTransportClient (settings).adddTransportAdress (InetAdress.getByName) "192.168.1.109"), 9300);QueryBuilder builder=QueryBuilders.multiMatchQuery('댐 수리 고강','documentname','uploader');SearchResponse response=client.prepareSearch(“tgdsm”) .setTypes(“document”) .setQuery(builder)//.setSize(3) .setFrom(0)//페이지 시작점.setSize(2)//페이지당 포함 수량.get();/System.out.println("결과"+response);*/SearchHits hits = response.getHits(); for (SearchHit hit : hits) { System.out.println(hit.getSourceAsString()); Map map = hit.getSourceAsMap(); for(String key:map.keySet()) {/System.out.println(key+"="+map.get(key));/} }
}

좋은 웹페이지 즐겨찾기