方法
通过http 9200访问
使用ES-Rest-Clients
单独创建模块
添加maven
1 2 3 4 5
| <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.4.2</version> </dependency>
|
编写配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.jhj.gulimall.search.config;
import org.apache.http.HttpHost; import org.elasticsearch.client.HttpAsyncResponseConsumerFactory; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class GulimallElasticSearchConfig { @Value("${es.ip}") private String ip; @Value("${es.port}") private Integer port;
public static final RequestOptions COMMON_OPTIONS;
static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build(); }
@Bean public RestHighLevelClient esRestClient() { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost(this.ip, this.port, "http")) ); return client; } }
|
使用
详细可以参考官方文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package com.jhj.gulimall.search;
import com.alibaba.fastjson.JSON; import com.jhj.gulimall.search.config.GulimallElasticSearchConfig; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource; import java.io.IOException;
@RunWith(SpringRunner.class) @SpringBootTest public class GulimallSearchApplicationTests {
class User{ private String username; private String gender; private Integer age;
public void setUsername(String username) { this.username = username; }
public void setGender(String gender) { this.gender = gender; }
public void setAge(Integer age) { this.age = age; } } @Resource private RestHighLevelClient restHighLevelClient; @Test
public void saveTest() throws IOException { IndexRequest users = new IndexRequest("users"); users.id("1");
User user = new User(); String s = JSON.toJSONString(user); user.setUsername("靳豪杰"); user.setAge(22); user.setGender("男"); users.source(s, XContentType.JSON);
IndexResponse index = restHighLevelClient.index(users, GulimallElasticSearchConfig.COMMON_OPTIONS); System.out.println(index); }
@Test
public void searchTest() throws IOException { SearchRequest searchRequest = new SearchRequest(); searchRequest.indices("users"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchRequest.source(searchSourceBuilder); restHighLevelClient.search(searchRequest,GulimallElasticSearchConfig.COMMON_OPTIONS); }
}
|
作者声明