"type": "keyword",
"index": false
},
"price": {
"type": "integer"
},
"score": {
"type": "integer"
},
"brand": {
"type": "keyword",
"copy\_to": "all"
},
"city": {
"type": "keyword"
},
"starName": {
"type": "keyword"
},
"business": {
"type": "keyword",
"copy\_to": "all"
},
"location": {
"type": "geo\_point"
},
"pic": {
"type": "keyword",
"index": false
},
"all": {
"type": "text",
"analyzer": "ik\_max\_word"
}
}
}
}
基于elasticsearch的规则, id用keyword
* 操作索引库
import com.zyw.elasticsearchdemo.constants.HotelConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class ElasticsearchDemoApplicationTests {
private RestHighLevelClient client;
/\*\*
* 删除索引库
*/
@Test
void deleteHotelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(“hotel”);
client.indices().delete(request, RequestOptions.DEFAULT);
}
/\*\*
* 判断索引库是否存在
*/
@Test
void existHotelIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest(“hotel”);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? “索引库已经存在” : “索引库不存在”);
}
/\*\*
* 创建索引库
*/
@Test
void createHotelIndex() throws IOException {
// 1.创建request对象
CreateIndexRequest request = new CreateIndexRequest(“hotel”);
// 2.准备请求的参数, DSL语句
request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
// 3. 发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
## RestClient操作文档
---
import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.mapper.HotelMapper;
import com.zyw.elasticsearchdemo.pojo.Hotel;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType