Elasticsearch是面向文档(document oriented)的,这意味着它可以存储整个对象或文档(document)。然而它不仅仅是存储(store),还会索引(index)每个文档的内容使之可以被搜索。在Elasticsearch中,你可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤。Elasticsearch比较传统关系型数据库
Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -> Indices -> Types -> documents -> Fields先安装docker,如果安装了宝塔面板可以直接在商店安装docker Docker安装Elasticsearch 拉取镜像并启动
#拉取镜像 docker pull elasticsearch:7.7.0 #启动镜像 docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.7.0 --name 表示镜像启动后的容器名称 -d: 后台运行容器,并返回容器ID; -e: 指定容器内的环境变量 -p: 指定端口映射,格式为:主机(宿主)端口:容器端口浏览器访问ip:9200 ElasticSearch面板
推荐浏览器安装Elasticvue插件管理ElasticSearch,Edge插件商店直接能安装,方便 。当然安装ElasticSearch Head也可。
Elasticvue | Edge插件商店
SpringBoot整合注:如果Linux是自己电脑的虚拟机SpringBoot能直接访问,但是如果Linux是腾讯云阿里云等云服务器就需要修改ElasticSearch的配置文件才能远程访问。不是云服务器请跳过
docker exec -it elasticsearch bash # docker命令进入elasticsearch vi config/elasticsearch.yml #通过vi编辑器打开配置文件 #文件内容如下 cluster.name: "docker-cluster" network.host: 0.0.0.0 #添加以下两行 表示所有ip能访问elasticsearch http.cors.enabled: true http.cors.allow-origin: "*" #保存文件 重启elasticsearch创建Maven工程 在pom文件导入坐标
pom.xml
4.0.0 cn.appmy appmy-springboot-es1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent2.1.4.RELEASE org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-data-elasticsearchorg.springframework.boot spring-boot-starter-testtest
在cn.appmy下创建启动类
@SpringBootApplication public class ESApplication { public static void main(String[] args) { SpringApplication.run(ESApplication.class, args); } }
创建yml文件
server: port: 8080 spring: data: elasticsearch: cluster-nodes: 127.0.0.1:9300 #如果是云服务器 填服务器ip 记得防火墙 cluster-name: elasticsearch创建pojo 添加注解进行映射
@document(indexName = "blog03",type = "article") public class Article implements Serializable { @Id private Long id; @Field(index = true,searchAnalyzer = "ik_smart",analyzer = "ik_smart",store = true,type = FieldType.Text) private String title; @Field(index = true,searchAnalyzer = "ik_smart",analyzer = "ik_smart",store = true,type = FieldType.Text) private String content; public Article() { } public Article(long id, String title, String content) { this.id = id; this.title = title; this.content = content; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Article{" + "id=" + id + ", title='" + title + ''' + ", content='" + content + ''' + '}'; } }创建Dao接口继承ElasticsearchRepository
public interface ArticleDao extends ElasticsearchRepository{ }测试
@SpringBootTest @RunWith(SpringRunner.class) public class EsApplicationTest03 { @Autowired private ElasticsearchTemplate elasticsearchTemplate; @Autowired private ArticleDao dao; //创建索引 //创建映射 @Test public void createMapping() { elasticsearchTemplate.createIndex(Article.class); elasticsearchTemplate.putMapping(Article.class); } }
原文博客https://appmy.cn/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)