ES的基本操作。

一:安装ES
注意:ES基于JAVA语言研发。环境依赖JDK。

下载路径:https://www.elastic.co/cn/start
两个节点:做es集群

ip节点
10.5.100.34node1
10.5.100.102node2

node1节点:

[root@node1 ~]# yum install java-1.8.0-openjdk.x86_64 -y
[root@node1 ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr
[root@node1 ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
[root@node1 ~]# 
[root@node1 ~]# yum install elasticsearch-7.9.0-x86_64.rpm -y     下载es的rpm包进行yum安装
[root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml    编辑es配置文件。添加两项
cluster.name: myes      定义的集群名称,默认是my-application
node.name: node1.shuo.com

[root@node1 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.5.100.34 node1 node1.shuo.com
[root@node1 ~]# systemctl restart elasticsearch
[root@node1 ~]# ss -tnl                 默认监听本机92009300
ES的默认端口;
1,参与集群的事务:9300/tcp
transport.tcp.port
2,接受请求:9200/tcp
 http.port
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      100                                  127.0.0.1:25                                                       *:*                  
LISTEN      0      128                           ::ffff:127.0.0.1:9200                                                    :::*                  
LISTEN      0      128                                        ::1:9200                                                    :::*                  
LISTEN      0      128                           ::ffff:127.0.0.1:9300                                                    :::*                  
LISTEN      0      128                                        ::1:9300                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
LISTEN      0      100                                        ::1:25                                                      :::*                  
[root@node1 ~]# 
[root@node1 ~]# systemctl stop firewalld  关闭防火墙设置

Node2节点

[root@node2 ~]# yum install java-1.8.0-openjdk.x86_64 -y
[root@node2 ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr
[root@node2 ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
[root@node2 ~]# 
[root@node2 ~]# yum install elasticsearch-7.9.0-x86_64.rpm -y     下载es的rpm包进行yum安装
[root@node2 ~]# vim /etc/elasticsearch/elasticsearch.yml    编辑es配置文件。添加两项
cluster.name: myes      定义的集群名称,默认是my-application
node.name: node2.shuo.com

[root@node2 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.5.100.34 node1 node1.shuo.com
[root@node2 ~]# systemctl restart elasticsearch
[root@node2 ~]# ss -tnl                 默认监听本机92009300
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      100                                  127.0.0.1:25                                                       *:*                  
LISTEN      0      128                           ::ffff:127.0.0.1:9200                                                    :::*                  
LISTEN      0      128                                        ::1:9200                                                    :::*                  
LISTEN      0      128                           ::ffff:127.0.0.1:9300                                                    :::*                  
LISTEN      0      128                                        ::1:9300                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
LISTEN      0      100                                        ::1:25                                                      :::*                  
[root@node2 ~]# 
[root@node2 ~]# systemctl stop firewalld 

二:ES的基础操作。

[root@node1 ~]# curl -X GET 'http://localhost:9200/_cluster/health?pretty'   检查集群的健康状态。pretty以优美的格式显示
{
  "cluster_name" : "myes",        集群名称
  "status" : "green",      集群状态,green表示正常。
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}
[root@node1 ~]# curl -X GET 'http://localhost:9200/_cluster/state/version?pretty' 查看集群的版本,及名称,
{
  "cluster_name" : "myes",
  "cluster_uuid" : "bGFEa9ZWTraeINTGo4VY3w",
  "version" : 73,
  "state_uuid" : "GjC8P9gbRJe4erB3_LC0zg"
}
[root@node1 ~]# 
[root@node1 ~]# curl -X GET 'http://localhost:9200/_cluster/stats/?pretty' 查看集群的所有状态信息。
{
  "_nodes" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "cluster_name" : "myes",
  "cluster_uuid" : "bGFEa9ZWTraeINTGo4VY3w",
  "timestamp" : 1601224533923,
  "status" : "green",
  "indices" : {
    "count" : 0,
    "shards" : { },
    "docs" : {
      "count" : 0,
      "deleted" : 0
    },

[root@node1 ~]#curl -X GET 'http://localhost:9200/_nodes/stats/?pretty'  查看集群个节点的相关信息。

三:ES的插件应用及CRUD。

(1)创建文档:
[root@node1 ~]# curl -H "Content-Type: application/json" -X PUT 'localhost:9200/students/class1/1?pretty' -d '
students:表示索引,class1表示文档。 id:表示索引,文档类型。  -H:指明头部  -d:表示指明文档。
> {                      
>   "first_name": "jing",
>   "last_name": "Guo",
>   "gender": "Male",
> "age": 25,                        
> "courses": "xianglong shiba zhang"
> }'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
[root@node1 ~]# 
(2)获取文档:
[root@node2 ~]# curl -XGET 'localhost:9200/students/class1/1?pretty'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "first_name" : "jing",
    "last_name" : "Guo",
    "gender" : "Male",
    "age" : 25,
    "courses" : "xianglong shiba zhang"
  }
}

(3)更新文档:
[root@node1 ~]# curl -H "Content-Type: application/json" -XPOST 'localhost:9200/students/class1/1/_update?pretty' -d'
{                     
  "doc" : {"age": 22 }
}'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
[root@node1 ~]# 
[root@node1 ~]# curl -XGET 'localhost:9200/students/class1/1?pretty'   查看年龄是否修改
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "first_name" : "jing",
    "last_name" : "Guo",
    "gender" : "Male",
    "age" : 22,
    "courses" : "xianglong shiba zhang"
  }
}
[root@node1 ~]# 
[root@node1 ~]# curl -XDELETE 'localhost:9200/students/class1/1?pretty'   删除文档类型1
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "_version" : 3,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}
[root@node1 ~]# curl -XGET 'localhost:9200/students/class1/1?pretty'
{
  "_index" : "students",
  "_type" : "class1",
  "_id" : "1",
  "found" : false     这里得false表示没有找到该文档类型
}
[root@node1 ~]# 
[root@node1 ~]# curl -XGET 'localhost:9200/_cat/indices?v'  查看索引的相关信息
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   students EJQ-7K5YTgCJlEOwN-01gA   1   1          0            0       228b           228b
[root@node1 ~]# 
[root@node2 ~]# curl -XDELETE 'localhost:9200/students?'   删除索引,文档随之也删除。
[root@node2 ~]# curl -XGET 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

四:ES的查询。

ES的查询阶段操作执行分为两个阶段;
  分散阶段:
  合并阶段:
[root@node1 ~]# curl -X GET 'localhost:9200/students/_search?pretty' 
{
  "took" : 11,   查询所需时长
  "timed_out" : false,  查询是否超时
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {     
    "total" : {    命中的文档
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,   最大命中率
    "hits" : [ ]   这个hits是个数组,本应该包括命中的文档,现在为空,说明还没有命中。
  }
}
[root@node1 ~]# 
[root@node1 ~]# curl -H "Content-Type: application/json" -X GET 'localhost:9200/students/_search?pretty' -d' 
另一种查询方式。如果要查询某个字段的
要在serarch后面指明查询条件。
>  {
>    "query": { "match_all": {} }
> }'
{
  "took" : 292,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
多索引,多类型查询;
/_search:查询所有索引的所有文档、
/index_name/_search:单索引
/index1,index2/_search:多索引;
/s* ,t*/_search:
/students/class1/_search:单类型搜索
/students/class1,class2/_search:多类型搜索

Mapping和Analysis
   ES:对每一个文档,会取得其所有域的所有值,生成一个名为all的域:执行查询时,如果在query_string未指定查询的域,则在_all域上执行查询操作。
   GET /_search?q='xianglong'
   GET /_search?q='xianglong%20shiba%20zhang'      %都是精确匹配。
   GET /_search?q=courses: 'xianglong%20shiba%20zhang'
   GET /_search?q=courses: 'xianglong'

前两个:表示在all域上搜索;
后两个:在指定的域上搜索:

四:ES内置的分析器

ES中搜索的数据广义上可被理解为两类。
type:exact
full-text:用于引用文本中数据,判断文档在多大程序上匹配查询请求,即评估文档与用户请求查询的相关度;
为了完成full-text搜索;es必须首先分析文本,并创建出倒排索引,倒排索引中的数据还需进行正规化为标准格式
分词,正规化,即分析

分析需要由分析器进行:analyzer
分析器由三部分组件构成:字符过滤器,分词器,分词过滤器

ES内置的分析器:
standard analyzer: 标准分析器。
simple analyzer
language analyzer

分析器不仅在创建索引时用到;在构建查询时也会用到。

request body:
分成两类:
query dsl:执行full-text查询时,基于相关度来评判匹配结果;查询执行过程复杂,且不会被缓存。
filter dsl:执行exact查询时,基于其结果为  "yes""no" 进行评判;速度快,且结果缓存。

filter dsl实例:
[root@node1 ~]# curl -H "Content-Type: application/json" -XGET 'localhost:9200/students/_search?pretty' -d'   term表示单项查找。
{
   "query": {
      "term": {
         "age": 23    
       }
    }
}'
{
  "took" : 49,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "students",
        "_type" : "class1",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "Rong",
          "last_name" : "huang",
          "gender" : "female",
          "age" : 23,
          "courses" : "luoying shenjian"
        }
      }
    ]
  }
}

term filter:用于多值精确匹配;  terms表示多项查找
   { "terms":  { "name" :  ["guo", "rong"]  }[root@node2 ~]# curl -H "Content-Type: application/json" -X GET 'localhost:9200/students/_search?pretty' -d'
{
  "query": {
     "terms": {
        "first_name": ["Rong","guo"]
      }
  }
}'
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
[root@node2 ~]# 

query  dsl:

     match_all  query:
        用于匹配所有文档,没有指定任何query,默认即为match_all query
        { "match_all": {}  }    

     match query: 在几乎任何域上执行full-text或exact-value查询;
     如果执行full-text查询;首先对查询时的语句做分析;
        {  "match":  {"students":  "Guo" }}
     如果执行exact-value查询;搜索精确值;此时,建议使用过滤,而非查询

     multi_match  query:用于在多个域上执行相同的查询;
        {  
            "multi_match":
                    "query":  full-text  serach
                    "field":  {'field1',  'field2' }
        }
        
        {  
            "multi_match":        这表示多域进行查询,表示在name和描述字段查找guo
                    "query":  {
                           "students":  "guo"
                      }
                    "field":
                         {
                            "name",
                            "description"
                         }
        }  
bool  query:
  基于boolean逻辑合并多个查询语句;与bool
  filter不同的是,查询字句不是返回yes 或 no  而是其计算出的匹配度分值,因此,query会为各子句合并其score

查询语句语法检查:
GET  /index/_validate/query?explain&pretty
{
    .....
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值