ElasticSearch
## ES
#### 1.保存数据(JSON)
`PUT http://192.168.56.10:9200/customer/external/1`
`{"name": "John Doe"}`

#### 2.查看数据
`GET http://192.168.56.10:9200/customer/external/1`

#### Query DSL语法
##### 查询所有match_all,分页每页5条记录,排序account_number降序,_source返回部分字段。全文检索按评分排序
```json
GET bank/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 5,
"sort": [
{
"account_number": {
"order": "desc"
}
}
],
"_source": ["age","balance"]
}
```
##### 单词匹配 任何一个【查询出 address 中包含 mill或者road 或者 mill road 的所有记录,并给出相关性得分】
```json
GET bank/_search
{
"query": {
"match": {
"address": "mill road"
}
}
}
```
##### 短语匹配【查出 address 中包含 mill road 的所有记录,并给出相关性得分】
```json
GET bank/_search
{
"query": {
"match_phrase": {
"address": "mill road"
}
}
}
```
##### 多字段匹配【state 或者 address 包含 mill】
```json
GET bank/_search
{
"query": {
"multi_match": {
"query": "mill",
"fields": [
"state",
"address"
]
}
}
}
```
##### 复合查询【address 包含 mill,并且 gender 是 M,如果 address 里面有 lane 最好不过,但是email 必须不包含 baluba.com】
```json
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "mill"
}
},
{
"match": {
"gender": "M"
}
}
],
"should": [
{
"match": {
"address": "lane"
}
}
],
"must_not": [
{
"match": {
"email": "baluba.com"
}
}
]
}
}
}
```
##### 结果过滤【并不是所有的查询都需要产生分数,特别是那些仅用于 “filtering”(过滤)的文档。】
```json
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "mill"
}
}
],
"filter": {
"range": {
"balance": {
"gte": 10000,
"lte": 20000
}
}
}
}
}
}
```
# ES整合Springboot
##### 导入依赖
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
```
##### ES配置文件
```java
@Configuration
public class GulimallElasticSearchConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer " + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient(@Value("${spring.elasticsearch.jest.uris}")String esUrl){
//TODO 修改为线上的地址
RestClientBuilder builder = null;
//final String hostname, final int port, final String scheme
// builder = RestClient.builder(new HttpHost("192.168.56.10", 9200, "http"));
builder = RestClient.builder(HttpHost.create(esUrl));
RestHighLevelClient client = new RestHighLevelClient(builder);
// RestHighLevelClient client = new RestHighLevelClient(
// RestClient.builder(
// new HttpHost("192.168.56.10", 9200, "http")));
return client;
}
}
```
##### 测试存储数据到es
```java
@Autowired
private RestHighLevelClient client;
@Test
public void indexData() throws IOException {
IndexRequest indexRequest = new IndexRequest("users");
indexRequest.id("1");//数据的id
// indexRequest.source("userName","zhangsan","age",18,"gender","男");
User user = new User();
user.setUserName("zhangsan");
user.setAge(18);
user.setGender("男");
String jsonString = JSON.toJSONString(user);
indexRequest.source(jsonString, XContentType.JSON);//要保存的内容
//执行操作
IndexResponse index = client.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
//提取有用的响应数据
System.out.println(index);
}
```