Elasticsearch笔记

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: Elasticsearch笔记

Elasticsearch与数据库比较


微信截图_20230222224619.png


安装Elasticsearch


安装Elasticsearch


安装kibana插件(推荐)



下载地址

https://www.elastic.co/cn/downloads/past-releases#kibana


安装Elasticsearch-head插件


安装node和npm


yum install -y nodejs npm


将Elasticsearch-head下载下来并且解压


GitHub - mobz/elasticsearch-head: A web front end for an elastic search cluster


7.png


进入 解压的目录 elasticsearch-head-master 下执行


npm install


8.jpg


在目录 elasticsearch-head-master 下执行启动es-head


npm run start


9.jpg


将Elasticsearch 和 Elasticsearch-head 绑定在一起


修改 /usr/local/elasticsearch-6.2.2/config/elasticsearch.yml文件


在最后添加一下配置


1. http.cors.enabled: true
2. http.cors.allow-origin: "*"


10.jpg


后台启动 Elasticsearch (esuser用户)(esuser用户)(esuser用户)


./bin/elasticsearch -d


11.jpg


启动Elasticsearch -head


npm run start


12.jpg


下下策:在箭头处自己输入


1.png


2.png


操作


添加


3.jpg


查询

查询一个

http://47.105.132.96:9200/people/man/1

查询全部

http://47.105.132.96:9200/people/man/_search

条件查询


4.jpg


添加


5.jpg


删除


6.jpg


SpringBoot用Jest操作Elasticsearch


POM


<dependency>
  <groupId>io.searchbox</groupId>
  <artifactId>jest</artifactId>
  <version>6.3.1</version>
</dependency>


application.properties


spring.elasticsearch.jest.uris=http://47.105.132.96:9200


Entity


package com.example.demo;
import io.searchbox.annotations.JestId;
public class Article {
  @JestId
  private Integer id;
  private String author;
  private String title;
  private String content;
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getAuthor() {
    return author;
  }
  public void setAuthor(String author) {
    this.author = author;
  }
  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;
  }
  public Article(Integer id, String author, String title, String content) {
    this.id = id;
    this.author = author;
    this.title = title;
    this.content = content;
  }
  public Article() {
  }
  @Override
  public String toString() {
    return "Article [id=" + id + ", author=" + author + ", title=" + title + ", content=" + content + "]";
  }
}


Controller


package com.example.demo;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.searchbox.client.JestClient;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
@RestController
public class ArticleController {
  @Autowired
  private JestClient jestClient;
  @RequestMapping("/select")
  public String selectArticle() {
    /*
     * { "query" : { "match" : { "author" : "zhangsan" } } }
     */
    String queryStr = "{\n" + "   \"query\" : {\n" + "     \"match\" : {\n" + "       \"author\" : \"zhangsan\"\n"
        + "    }\n" + "}\n" + "}";
    // 构建搜索功能
    Search index = new Search.Builder(queryStr).addIndex("cbeann").addType("news").build();
    try {
      SearchResult result = jestClient.execute(index);
      return result.getJsonString();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      return "error";
    }
  }
  @RequestMapping("/add")
  public String addArticle() {
    Article a = new Article(100, "zhangsan", "请吃饭", "2019年1月1日我请吃饭");
    Index index = new Index.Builder(a).index("cbeann").type("news").build();
    try {
      jestClient.execute(index);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.out.println("--------出错-----------");
    }
    return "success";
  }
  @RequestMapping("/update")
  public String updateArticle() {
    // 添加ID相同的就会覆盖
    return "success";
  }
  @RequestMapping("/delete")
  public String deleteArticle() {
    // 还未涉及
    return "success";
  }
}


SpringBoot用SpringDate操作Elasticsearch(注意:版本对应很重要)


SpringBoot 1.5.12   +   elasticsearch2.4.6


Docker下载2.4.6


docker pull elasticsearch:2.4.6


docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name myes 5e9d896dcc


pom


<?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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.12.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>ElasticsearchJestDemo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
    <!-- 根据你的Elasticsearch 的版本选择 jest 的版本 ,比如 你的 ES 是6.x.x ,jest的版本可以使 6.x.x -->
    <dependency>
      <groupId>io.searchbox</groupId>
      <artifactId>jest</artifactId>
      <version>6.3.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>


application.properties


spring.elasticsearch.jest.uris=http://47.105.132.96:9200
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=47.105.132.96:9300


实体类


package com.example.demo;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName="cbeann",type="books")
public class Book {
  private Integer id;
  private String name;
  private String auther;
  @Override
  public String toString() {
    return "Book [id=" + id + ", name=" + name + ", auther=" + auther + "]";
  }
  public Book(Integer id, String name, String auther) {
    super();
    this.id = id;
    this.name = name;
    this.auther = auther;
  }
  public Book() {
    super();
  }
  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getAuther() {
    return auther;
  }
  public void setAuther(String auther) {
    this.auther = auther;
  }
}


Repository(类似JPA)


package com.example.demo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, Integer>{
}


BookController


package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {
  @Autowired
  private BookRepository bookRepository;
  @RequestMapping("/add")
  public String add(){
    Book book=new Book(1, "西游记", "吴承恩");
    bookRepository.index(book);
    return "success";
  }
}


或者


@RestController
public class OtherController {
  @Autowired
  private ElasticsearchTemplate elasticsearchTemplate;
}



相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
5月前
|
安全
【Elasticsearch6】安装笔记
【Elasticsearch6】安装笔记
37 2
|
6月前
|
关系型数据库 MySQL 索引
|
6月前
|
前端开发 Java iOS开发
elasticsearch8.1源码编译笔记
elasticsearch8.1源码编译笔记
137 0
|
6月前
|
自然语言处理 安全 关系型数据库
|
JSON 安全 搜索推荐
白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧(一)
白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧(一)
1011 0
|
存储 Web App开发 搜索推荐
Elasticsearch笔记(集群插件、kibana、什么是倒排索引)
Elasticsearch笔记(集群插件、kibana、什么是倒排索引)
Elasticsearch笔记(集群插件、kibana、什么是倒排索引)
Elasticsearch核心技术与实战-极客课程笔记
Elasticsearch核心技术与实战-极客课程笔记
429 0
Elasticsearch核心技术与实战-极客课程笔记
|
搜索推荐
ElasticSearch笔记
ElasticSearch笔记
138 0
ElasticSearch笔记
|
SQL 缓存 自然语言处理
白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧(二)
白日梦的Elasticsearch实战笔记,32个查询案例、15个聚合案例、7个查询优化技巧(二)
941 1
|
JSON 安全 Java
白日梦的Elasticsearch实战笔记,ES账号免费借用、32个查询案例、15个聚合案例、7个查询优化技巧。(一)
白日梦的Elasticsearch实战笔记,ES账号免费借用、32个查询案例、15个聚合案例、7个查询优化技巧。(一)
264 0