Elasticsearch核心概念
MYSQL(relational DB) | Databases(数据库) | Tables(表) | Rows(行) | Columns(列) |
---|---|---|---|---|
Elasticsearch | Indexes(索引) | Types(类型) | Documents(文档) | Fields(字段) |
创建索引库 :
请求方式:PUT
请求url:http://localhost:9200/blog1
创建索引时配置映射
请求方式:POST
请求url:http://127.0.0.1:9200/blog1/hello/_mapping
删除索引
请求方式:DELETE
请求url:http://localhost:9200/blog1
创建文档
请求方式:POST
请求url:http://localhost:9200/blog1/article/1
修改文档
请求方式:POST
请求url:http://localhost:9200/blog1/article/1
删除文档
请求方式:DELETE
请求url:http://localhost:9200/blog1/article/1
根据id查询
请求方式:GET
请求url:http://localhost:9200/blog1/article/1
querystring查询
请求方式:POST
请求url:http://localhost:9200/blog1/article/_search
请求体:
{ "query": { "query_string": { "default_field": "title", "query": "搜索服务器" } } }
term查询
请求方式:POST
请求url:http://localhost:9200/blog1/article/_search
请求体:
{ "query": { "term": { "title": "搜索" } } }
集群配置
#节点3的配置信息:
#集群名称,保证唯一 cluster.name: my-Elasticsearch
#节点名称,必须不一样 node.name: node-3
#必须为本机的ip地址 network.host: 127.0.0.1
#服务端口号,在同一机器下必须不一样 http.port: 9203
#集群间通信端口号,在同一机器下必须不一样 transport.tcp.port: 9303
#设置集群自动发现机器ip集合 discovery.zen.ping.unicast.hosts: ["127.0.0.1:9301","127.0.0.1:9302","127.0.0.1:9303"]
springData与es整合
导入Spring Data ElasticSearch坐标
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>itheima_elasticsearch_demo3</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.24</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
2)创建applicationContext.xml配置文件,引入elasticsearch命 名空间
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>3.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
</dependencies>
</project>
创建applicationContext.xml配置文件,引入elasticsearch命 名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation="
3)编写实体Article
4)编写Dao
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/springelasticsearch-1.0.xsd">
</beans>
编写实体Article
编写Dao
配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
7)配置实体
基于spring data elasticsearch注解配置索引、映射和实体的关系
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/springelasticsearch-1.0.xsd ">
<!-- 扫描Dao包,自动创建实例 -->
<elasticsearch:repositories base-package="com.itheima.dao"/>
<!-- 扫描Service包,创建Service的实体 -->
<context:component-scan base-package="com.itheima.service"/>
<!-- 配置elasticSearch的连接 -->
<!-- 配置elasticSearch的连接 -->
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" cluster-name="elasticsearch"/>
<!-- ElasticSearch模版对象 -->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"></constructor-arg>
</bean>
</beans>
配置实体
创建测试类SpringDataESTest