安装
一、elasticsearch-7.6.1-windows-x86_64安装与配置
0.解压
1.解决跨域问题
在config\elasticsearch.yml里追加
http.cors.enabled: true
http.cors.allow-origin: "*"
2.分词插件安装
将elasticsearch-analysis-ik-7.6.1.zip拖到elasticsearch-7.6.1\plugins\ik解压出来
3.在bin里找到elasticsearch.bat可直接运行
二、elasticsearch-head-master启动
0.确保安装nodejs并且配置到环境变量
1.npm install
2.npm run start (这里和平时启动不一样)
三、kibana-7.6.1-windows-x86_64安装与配置
0.解压
1.在\config\kibana.yml追加。国际化切换中文配置。
i18n.locale: "zh-CN"
2.\bin\kibana.bat 运行
词法分析
GET _analyze
{
"analyzer" : "ik_smart" ,
"text" : "控制台开发工具"
}
GET _analyze
{
"analyzer" : "ik_max_word" ,
"text" : "草草草123"
}
CRUD
增
PUT class / _doc/ 1
{
"name" : "穷哈哈" ,
"age" : 24
}
PUT class / _doc/ 2
{
"name" : "张三" ,
"age" : 21
}
PUT class / _doc/ 3
{
"name" : "王五" ,
"age" : 32
}
POST class / _doc
{
"name" : "李四" ,
"age" : 64
}
删
DELETE class / _doc/ 3
改
POST class / _update/ 1
{
"doc" : {
"age" : 18
}
}
查
GET class / _doc/ 1
GET class / _search? q= name: 李四
GET class / _search? q= name: ( 张三 or 李四)
GET class / _search? q= name: ( NOT 李四)
GET class / _search? q= age: ( >= 20 AND <= 30 )
GET class / _search? q= name: * 三
GET class / _search
{
"from" : 0 ,
"size" : 2
}
Spring Boot整合ES
创建工程时选中NoSQL中的 Spring Data Elasticsearch
pom.xml
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-data-elasticsearch</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> fastjson</ artifactId>
< version> 1.2.78</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
配置类
import org. apache. http. HttpHost ;
import org. elasticsearch. client. RestClient ;
import org. elasticsearch. client. RestHighLevelClient ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
@Configuration
public class ESConfiguration {
@Bean
public RestHighLevelClient restHighLevelClient ( ) {
RestHighLevelClient client = new RestHighLevelClient (
RestClient . builder (
new HttpHost ( "localhost" , 9200 , "http" )
)
) ;
return client;
}
}
傻逼方式一
建实体类
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String name;
}
各种方法
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
void contextLoads ( ) throws Exception {
CreateIndexRequest request = new CreateIndexRequest ( "test_index" ) ;
CreateIndexResponse response = restHighLevelClient. indices ( )
. create ( request, RequestOptions . DEFAULT) ;
System . out. println ( response. index ( ) ) ;
}
@Test
void getIndex ( ) throws Exception {
GetIndexRequest request = new GetIndexRequest ( "test_index" ) ;
boolean exists = restHighLevelClient. indices ( ) . exists ( request, RequestOptions . DEFAULT) ;
System . out. println ( exists) ;
}
@Test
void deletIndex ( ) throws Exception {
DeleteIndexRequest request = new DeleteIndexRequest ( "test_index" ) ;
AcknowledgedResponse delete = restHighLevelClient. indices ( ) . delete ( request, RequestOptions . DEFAULT) ;
System . out. println ( delete. isAcknowledged ( ) ) ;
}
@Test
void add ( ) throws Exception {
User user = new User ( 1 , "wwy" ) ;
IndexRequest request = new IndexRequest ( "mytest" ) ;
request. id ( "1" ) ;
request. source ( JSON. toJSONString ( user) , XContentType . JSON) ;
IndexResponse response = restHighLevelClient. index ( request, RequestOptions . DEFAULT) ;
System . out. println ( response. toString ( ) ) ;
System . out. println ( response. status ( ) ) ;
}
@Test
void get ( ) throws Exception {
GetRequest request = new GetRequest ( "mytest" , "1" ) ;
GetResponse response = restHighLevelClient. get ( request, RequestOptions . DEFAULT) ;
System . out. println ( response. getSourceAsString ( ) ) ;
System . out. println ( response. getSource ( ) ) ;
}
@Test
void update ( ) throws Exception {
UpdateRequest request = new UpdateRequest ( "mytest" , "1" ) ;
User user = new User ( 1 , "李四" ) ;
request. doc ( JSON. toJSONString ( user) , XContentType . JSON) ;
UpdateResponse response = restHighLevelClient. update ( request, RequestOptions . DEFAULT) ;
System . out. println ( response. status ( ) ) ;
}
@Test
void delete ( ) throws Exception {
DeleteRequest request = new DeleteRequest ( "mytest" , "1" ) ;
DeleteResponse response = restHighLevelClient. delete ( request, RequestOptions . DEFAULT) ;
System . out. println ( response. status ( ) ) ;
}
牛逼方式二
建实体类
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import org. springframework. data. annotation. Id ;
import org. springframework. data. elasticsearch. annotations. Document ;
import org. springframework. data. elasticsearch. annotations. Field ;
import org. springframework. data. elasticsearch. annotations. FieldType ;
import java. util. Date ;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document ( indexName = "order" )
public class Order {
@Id
private Integer id;
@Field ( type = FieldType. Text , analyzer = "ik_max_word" )
private String name;
private Date date;
}
创建接口
import com. ele. entity. Order ;
import org. springframework. data. elasticsearch. repository. ElasticsearchRepository ;
public interface EsOrderRepository extends ElasticsearchRepository < Order , Integer > {
}
各种方法
@Autowired
private EsOrderRepository esOrderRepository;
@Test
void newadd ( ) {
Date date = new Date ( ) ;
Order order = new Order ( 1 , "男士背包" , date) ;
Order save = esOrderRepository. save ( order) ;
System . out. println ( save) ;
}
@Test
void newget ( ) {
Iterable < Order > all = esOrderRepository. findAll ( ) ;
Iterator < Order > iterator = all. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
System . out. println ( iterator. next ( ) ) ;
}
}
@Test
void newupdate ( ) {
esOrderRepository. deleteById ( 1 ) ;
}