内网5.4.1版本比较低,不支持外网的7.13.2的api调用,只能直接通过curl请求来操作。
    首先创建索引,可以直接创建,删除,不过一般都是需要mappings和settings,所以创建的时候需要指定。

    1. public boolean addEsIndex(String indexName){
    2. RestTemplate restTemplate = new RestTemplate();
    3. String ESurl = "http://localhost:9200/" + indexName;
    4. JSONObject body = getProperties();
    5. ResponseEntity<String> exchange = null;
    6. try{
    7. HttpHeaders httpHeaders = new HttpHeaders();
    8. httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    9. HttpEntity<Map<String, Object>> mapHttpEntity = new HttpEntity<>(body, httpHeaders);
    10. exchange = restTemplate.exchange(ESurl, HttpMethod.PUT, mapHttpEntity, String.class);
    11. return true;
    12. }catch (Exception e){
    13. e.getStackTrace();
    14. return false;
    15. }
    16. }
    17. public JSONObject getProperties(){
    18. return new JSONObject(true);
    19. }

    对于mappings和settings的具体配置可以网上搜索,mappings的下一级要定义type,便于后续插入文档时指定type
    判断索引是否存在

    1. public boolean isExist(String indexName){
    2. String ESurl = "http://localhost:9200/" + indexName;
    3. HttpClient httpClient = HttpClients.createDefault();
    4. HttpGet httpGet = new HttpGet(ESurl);
    5. HttpResponse response = null;
    6. try{
    7. response = httpClient.execute(httpGet);
    8. }catch (IOException e){
    9. e.printStackTrace();
    10. }
    11. return response.getStatusLine().getStatusCode() == 200;
    12. }

    删除索引

    1. public boolean deleteESIndex(String indexname){
    2. RestTemplate restTemplate = new RestTemplate();
    3. String ESUrl = "http://localhost:9200/" + indexname;
    4. try{
    5. restTemplate.delete(ESUrl);
    6. return true;
    7. }catch (RestClientException e){
    8. e.printStackTrace();
    9. return false;
    10. }
    11. }

    添加文档

    1. public boolean insertEsDocument(String indexname, JSONObject body, String insertId){
    2. RestTemplate restTemplate = new RestTemplate();
    3. String ESurl = "http://localhost:9200/" + indexname + "/" + "type" + "/" + insertId;
    4. ResponseEntity<String> exchange = null;
    5. try{
    6. HttpHeaders httpHeaders = new HttpHeaders();
    7. httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    8. HttpEntity<Map<String, Object>> mapHttpEntity = new HttpEntity<>(body, httpHeaders);
    9. exchange = restTemplate.exchange(ESurl, HttpMethod.POST, mapHttpEntity, String.class);
    10. return true;
    11. }catch (Exception e){
    12. e.getStackTrace();
    13. return false;
    14. }
    15. }

    根据id删除一条文档

    1. public boolean deleteEsDocumentById(String indexName, String id){
    2. RestTemplate restTemplate = new RestTemplate();
    3. String ESUrl = "http://localhost:9200/" + indexName + "/" + "type" + "/" + id;
    4. try{
    5. restTemplate.delete(ESUrl);
    6. return true;
    7. }catch (Exception e){
    8. e.printStackTrace();
    9. return false;
    10. }
    11. }

    最大的叫索引,索引下面有个类型叫type创建索引的时候mapping下一级需要指定,类型下面一条数据称为一个文档。具体mappings和settings网上搜。
    一般用到的话都会给。最好mappings里面指定的字段和添加文档时的字段都要一一对应。导包。

    1. import com.alibaba.fastjson.JSONObject;
    2. import org.apache.http.HttpResponse;
    3. import org.apache.http.client.HttpClient;
    4. import org.apache.http.client.methods.HttpGet;
    5. import org.apache.http.impl.client.HttpClients;
    6. import org.junit.runner.RunWith;
    7. import org.springframework.boot.test.context.SpringBootTest;
    8. import org.springframework.http.*;
    9. import org.springframework.test.context.junit4.SpringRunner;
    10. import org.springframework.web.client.RestClientException;
    11. import org.springframework.web.client.RestTemplate;
    12. import java.io.IOException;
    13. import java.util.Map;