ElasticSearch的使用

Posted 去糖不加冰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearch的使用相关的知识,希望对你有一定的参考价值。

一、安装ElasticSearch

下载连接:https://www.elastic.co/cn/elasticsearch/
之后解压文件,通过命令cd到bin目录下,输入elasticsearch.bat,启动服务

之后在浏览器中输入http://localhost:9200,当看到以下信息说明服务启动成功


  "name" : "DESKTOP-1IK0UC3",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "fhhG4xtCT0mZ6ogsze66zA",
  "version" : 
    "number" : "7.14.2",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "6bc13727ce758c0e943c3c21653b3da82f627f75",
    "build_date" : "2021-09-15T10:18:09.722761972Z",
    "build_snapshot" : false,
    "lucene_version" : "8.9.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  ,
  "tagline" : "You Know, for Search"

需要注意的是elasticsearch是java编写的所以运行需要jdk的环境,elasticsearch目录下有一个jdk的文件夹,如果没有是需要自己配置jdk的环境。

二、Kibana

为了方便学习使用这里推荐使用Kibana,在对应的官网上找到Kibana选项然后下载Kibana,Kibana与elasticsearch的版本要对应上
同样的操作,进入到bin文件夹内执行kibana.bat,一般Kibanna默认的端口号是5601
通过浏览器输入http://localhost:5601/,会进入到一个图形界面选择菜单栏里的
Dev Tools

三、创建

Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。

//通过post方式直接创建数据
//创建索引为shopping的数据
POST /shopping/_doc

  "title":"手机",
  "name":"华为mate40",
  "images":"http://www.xxx.com/xx.jpg",
  "price":7999.00

//elasticsearch返回

  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "AdU3aIABsHOWi0z2JXR6",//此id为elasticSearch生成的
  "_version" : 1,
  "result" : "created",
  "_shards" : 
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  ,
  "_seq_no" : 10,
  "_primary_term" : 2

不使用elasticsearch生成的id,需要自行指定id

//里面的3就是指定的id
POST /shopping/_doc/3

  "title":"数码产品",
  "name":"金士顿32GU盘",
  "images":"http://www.xxx.com/xx.jpg",
  "price":45.00

//返回的数据

  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "3",
  "_version" : 1,
  "result" : "created",
  "_shards" : 
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  ,
  "_seq_no" : 11,
  "_primary_term" : 2

当然指定了id也可以使用put方式过程类似

四、更新

1.覆盖更新(此方式不太推荐用于更新)
例如:修改id为10的内容

PUT /shopping/_doc/10

  "title":"生鲜水果",
  "name":"苹果",
  "images":"http://www.xxx.com/xx.jpg",
  "price":10.00

2.局部更新(一般的更新只会更新某些字段的内容,所以更新采用局部更新的方式比较合适)
例如更新id为10中name的内容

POST /shopping/_update/10

  "doc":
    "name":"芒果"
  

五、删除数据

例如删除id为2的数据

DELETE /shopping/_doc/2

六、查询

查讯采用GET的方式
1.查询索引的全部数据
通过GET方式索引名称,_search

GET /shopping/_search

2.基于id查询,例如查询id为3的数据

GET /shopping/_doc/3

3.条件查询
(1)、match方式

match查询属于全文查询,在查询时,ES会先分析查询字符串,然后根据分词构建查询。

例如查询title为"红米"的数据

GET /shopping/_search

  "query":   
    "match":
      "title":"红米"
    
  

查询结果

"hits" : [
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 2.3367887,
        "_source" : 
          "title" : "红米手机",
          "category" : "红米",
          "images" : "http://www.xiaomi.com/xm.jpg",
          "price" : 1999.0
        
      ,
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "10",
        "_score" : 1.7820551,
        "_source" : 
          "title" : "红米电视",
          "name" : "红米max11",
          "images" : "http://www.xxx.com/xx.jpg",
          "price" : 3999.0
        
      ,
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "0kq9VYABBlS6ycwT5aie",
        "_score" : 1.3246095,
        "_source" : 
          "title" : "小米手机",
          "category" : "小米",
          "images" : "http://www.xiaomi.com/xm.jpg",
          "price" : 3099.0
        
      ,
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "AdU3aIABsHOWi0z2JXR6",
        "_score" : 0.70679253,
        "_source" : 
          "title" : "手机",
          "name" : "华为mate40",
          "images" : "http://www.xxx.com/xx.jpg",
          "price" : 7999.0
        
      
      ]

(2)、match_phrase方式

match_phrase在查询时也会先分析查询字符串,然后对这些词项进行搜索,不同的是match_phrase查询只会保留包含全部查询字符串的文档

GET /shopping/_search

  "query": 
    "match_phrase":
      "title":"红米手机"
    
  

查询结果

"hits" : [
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "00q_VYABBlS6ycwTPKiV",
        "_score" : 2.3367884,
        "_source" : 
          "title" : "红米手机",
          "category" : "红米",
          "images" : "http://www.xiaomi.com/xm.jpg",
          "price" : 1999.0
        
      
      ]

(3)、bool方式

1.must等同于and
2.must_not等同于NOT
3.should等同于OR
4.filter过滤

must的应用
例如有两行数据,查询价格为45,标题是"数码产品"的数据

[ 
		
          "title" : "护肤产品",
          "name" : "洗面奶",
          "images" : "http://www.xxx.com/xx.jpg",
          "price" : 45.0
        ,
        
          "title" : "数码产品",
          "name" : "金士顿32GU盘",
          "images" : "http://www.xxx.com/xx.jpg",
          "price" : 45.0
        
]

查询语法

GET /shopping/_search

  "query": 
    "bool":
      "must":[
        
          "term":
            "price":45
          
        ,
        
          "match_phrase": 
            "title":"数码产品"
          
        
      ]
    
  

查询结果

"hits" : [
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 7.725004,
        "_source" : 
          "title" : "数码产品",
          "name" : "金士顿32GU盘",
          "images" : "http://www.xxx.com/xx.jpg",
          "price" : 45.0
        
      
    ]

另外两种:must_not、should语法是类似的就不再这里展开了

query context:会对搜索进行相关性算分值,
而filter context :不需要相关性算分,能够利用缓存来获得更好的性能
例如

must查询会有一个:“max_score” : 7.725004,
而换成filter:“max_score” : 0.0

filter
(4)、范围查询

range范围查询支持多种范围类型
1.数值类型
2.日期类型(timestamp)
3.ip地址类型

range的表达式
gt:大于
lt:小于
gte:大于等于
lte:小于等于
例如查询价格大于1999小于5000的商品

GET /shopping/_search

  "query": 
    "range":
      "price": 
        "gt": 1999,
        "lt": 5000
      
    
  

range也是可以搭配bool,和filter一块使用,来满足不同的查询需求。

七、分页

from:是启始位置
size:是需要查询的行数

例如全查询标题为"手机",价格大于1999小于5000的商品,并分页(由于我没有填充太多数据这里按每页一行)。

GET /shopping/_search

  "query": 
    "bool":
      "must": [
        
          "match":
            "title":"手机"
          
        
      ], 
      "filter":
          "range":
            "price": 
            "gt": 1999,
            "lt": 5000
            
          
      
      
    
  ,
    "from":0,
    "size":1

输出结果


  "took" : 0,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 
      "value" : 2, //总数据量
      "relation" : "eq"
    ,
    "max_score" : 0.83024156,
    "hits" : [
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.83024156,
        "_source" : 
          "title" : "vivo手机",
          "category" : "vivo",
          "images" : "http://www.xiaomi.com/xm.jpg",
          "price" : 4999.0
        
      
    ]
  

八、查询需要的字段内容

_source:指定字段返回

GET /shopping/_search

  "query": 
      "match_phrase": 
        "title": "数码产品"
      
  ,
  "_source":["name","price"]

输出结果

"hits" : [
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 6.725004,
        "_source" : 
          "price" : 45.0,
          "name" : "金士顿32GU盘"
        
      
    ]

九、排序

sort:排序
order:asc正序,desc逆序

按照分值逆序_score

GET /shopping/_search

  "query": 
      "match": 
        "title": "手机"
      
  ,
  "sort":
    "_score":
      "order":"desc"
    
  

输出结果

"hits" : [
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "AdU3aIABsHOWi0z2JXR6",
        "_score" : 0.943285,
        "_source" : 
          "title" : "手机",
          "name" : "华为mate40",
          "images" : "http://www.xxx.com/xx.jpg",
          "price" : 7999.0
        
      ,
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.83024156,
        "_source" : 
          "title" : "vivo手机",
          "category" : "vivo",
          "images" : "http://www.xiaomi.com/xm.jpg",
          "price" : 4999.0
        
      
      ]

十、搜索内容高亮

给搜索的字段加粗
通过highlight指定字段
pre_tags:开头
post_tags:结尾

GET /shopping/_search

  "query": 
      "match": 
        "title": "手机"
      
  ,
  "highlight": 
    "fields":
      "title":
        "pre_tags": [
          "<em sytle='color:red;'>"
          ],
          "post_tags": [
            "</em>"
            ]
      
    
  

输出结果

"hits" : [
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "AdU3aIABsHOWi0z2JXR6",
        "_score" : 0.943285,
        "_source" : 
          "title" : "手机",
          "name" : "华为mate40",
          "images" : "http://www.xxx.com/xx.jpg",
          "price" : 7999.0
        ,
        "highlight" : 
          "title" : [
            "<em sytle='color:red;'>手</em><em sytle='color:red;'>机</em>"
          ]
        
      ,
      
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.83024156,
        "_source" : 
          "title" : "vivo手机",
          "category" : "vivo",
          "images" : "http://www.xiaomi.com/xm.jpg",
          "price" : 4999.0
        ,
        "highlight" : 
          "title" : [
            "vivo<em sytle='color:red;'>手</em><em sytle='color:red;'>机</em>"
          ]
        
      
      ]

扩展:对于全文搜索可能还需要加入IK分词器插件。

以上是关于ElasticSearch的使用的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch学习之多种查询方式

CentOS 安装 elasticsearch 注意点

Elasticsearch 顶尖高手--快速入门案例实战:多种搜索方式

Elasticsearch 8.X 导出 CSV 多种方案,一网打尽!

Elasticsearch 8.X 导出 CSV 多种方案,一网打尽!

Elasticsearch 8.X 导出 CSV 多种方案,一网打尽!