【ES】Elasticsearch的基本操作及相关概念-01

前言

所有操作基于kibana的 devs tools,相关的安装可以看:

【ES学习】ElasticSearch MacOS版 安装与使用(图文教程)

【ES学习】ElasticSearch在Kibana的使用 Kibana安装(MacOS版)

json数据形式

ES数据通过JSON形式存储

在这里插入图片描述

创建文档

PUT /索引index/类型type/文档id { JSON语句 }

我们输入:

PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

在这里插入图片描述

继续加入一些文档:

PUT /megacorp/employee/2
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}

PUT /megacorp/employee/3
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

检索文档 /index/type/id

GET /索引index/类型type/文档id

我们输入:

GET /megacorp/employee/1

得到:

{
  "_index" : "megacorp",
  "_type" : "employee",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "first_name" : "John",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I love to go rock climbing",
    "interests" : [
      "sports",
      "music"
    ]
  }
}

_index:表示索引的名称
_type:表示类型名称
_id:表示文档id。
_version:表示这条文档的版本,因为我们没有修改过,所以版本是1,修改过之后会递增。
_seq_no:是序列号。
_primary_term:和_seq_no一样是一个整数,每当Primary Shard发生重新分配时,比如重启,Primary选举等,_primary_term会递增1。
found:查找成功为true,不成功为false。
_source:数据源,JSON本体

轻量搜索 /index/type/_search

我们输入:

GET /megacorp/employee/_search

得到:

{
  "took" : 23,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      },
      .....
      ..... (以下省略)

took:耗时
timed_out:是否超时
_shards.total:分片总数

hits.total:查询到的数量
hits.max_score:匹配度分数最大值
hits.hits:查询到的结果
hits._score:匹配度分数

查询概述

查询主要分为字段类查询和复合查询。

  • 字段类查询:只针对某一个字段进行查询,如match、term等;
  • 复合查询:可以进行一个或多个字段的查询,如bool查询等.

字段类查询主要包括两类:单词匹配和全文匹配。

  • 单词匹配(Term Level Query):不对查询语句进行分词处理,直接匹配该字段的倒排索引;单词匹配包括term、terms、range、exist、ids、prefix、fuzzy、wildcard等查询语句:
  • 全文匹配(Full Text Query):对指定的text类型的字段进行全文检索,会先对查询语句进行分词处理,例如你输入的查询文本是"我在马路边",es在分词器的作用下就会分词为"我"、“在”、"马路"这么几个单词,然后再去匹配。全文匹配包括match、match_phrase、multi_match、match_phrase_prefix、query_string、simple_query_string等查询语句;

query查询: match

match是最基本的基于全文检索的字段类查询语法

还记得我们的数据:

在这里插入图片描述

在字段about中找出所有包含like单词,我们输入:

GET /megacorp/employee/_search
{
  "query" : {
    "match": {
      "about": "like"
    }
  }
}

得到:

{
  "took" : 101,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.49376786,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "3",
        "_score" : 0.49376786,
        "_source" : {
          "first_name" : "Douglas",
          "last_name" : "Fir",
          "age" : 35,
          "about" : "I like to build cabinets",
          "interests" : [
            "forestry"
          ]
        }
      },
      ......
      ......(以下省略)

我们可以得到两条数据,他们中的about的字段都包含like这个词。

如果我们输入:

GET /megacorp/employee/_search
{
  "query" : {
    "match": {
      "about": "like climbing"
    }
  }
}

则会显示全部三个结果,说明单词之间是默认以"或"逻辑搜索的,而不需要真的出现"like climbing"这个词。

query查询: match - operator

query查询的match API还可以通过operator关键字,指定多个搜索词关系的匹配。(如果不指定operator就默认是“或”逻辑)

我们输入:

======== 多参数搜索表达式 ==========
GET /索引/类型/_search
{
  "query" : {
    "match": {
      "搜索文档字段" : {
        "query": "搜索单词",
        "operator": "逻辑关系"
      }
    }
  }
}

===================================
GET /megacorp/employee/_search
{
  "query" : {
    "match": {
      "about" : {
        "query": "go climbing",
        "operator": "and"
      }
    }
  }
}

这里我们的operator是“与”逻辑关系,也就是说,必须是about字段中同时出现goclimbing单词的文档doc,才能被搜索出来。

得到:

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.9155619,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "1",
        "_score" : 1.9155619,
        "_source" : {
          "first_name" : "John",
          "last_name" : "Smith",
          "age" : 25,
          "about" : "I love to go rock climbing",
          "interests" : [
            "sports",
            "music"
          ]
        }
      }
    ]
  }
}

query查询: match - minimum_should_match

minimum_should_match可以规定搜索词序列的最少包含个数。

比如此时搜索词序列为"to rock climbing",搜出来的文档必须至少包含to rock,或者rock climbing,只单独包含to的文档是搜不到的。

我们输入:

GET /megacorp/employee/_search
{
  "query" : {
    "match": {
      "about" : {
        "query": "to rock climbing",
        "minimum_should_match": 2
      }
    }
  }
}

得到:

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    ......
    ......

query查询: match_phrase

match_phrase也是对字段进行检索。

和match的区别在于:match是无序的,match_query是有顺序要求的。

我们输入:

GET /megacorp/employee/_search
{
  "query" : {
    "match_phrase": {
      "about" : "climbing rock"
    }
  }
}

结果什么都没搜出来。

query查询: match_phrase - slop

match_phrase可以添加参数slop,控制单词之间的允许间隔

GET /megacorp/employee/_search
{
  "query" : {
    "match_phrase": {
      "about" : {
        "query": "climbing rock",
        "slop": 2
      }
    }
  }
}

这次能搜出结果了。

query查询: query_string

query_string和 query查询的 match - operator 语句很像,也是在指定字段中搜索指定的(一串)搜索单词,并且可以用"与或非"逻辑关系加以限制。

搜索结果在about字段中,不要有go 和 rock,我们输入:

=========== 表达式 =============
GET /megacorp/employee/_search
{
  "query" : {
    "query_string": {
      "default_field": "目标字段",
      "query": "搜索单词串 + AND/OR/NOT等逻辑"
    }
  }
}

==============================
GET /megacorp/employee/_search
{
  "query" : {
    "query_string": {
      "default_field": "about",
      "query": "NOT go AND rock"
    }
  }
}

得到:

{
  "took" : 64,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.4589591,
    "hits" : [
      {
        "_index" : "megacorp",
        "_type" : "employee",
        "_id" : "2",
        "_score" : 0.4589591,
        "_source" : {
          "first_name" : "Jane",
          "last_name" : "Smith",
          "age" : 32,
          "about" : "I like to collect rock albums",
          "interests" : [
            "music"
          ]
        }
      }
    ]
  }
}

也可以多字段进行搜索:

GET /megacorp/employee/_search
{
  "query" : {
    "query_string": {
      "fields": ["about", "last_name"],
      "query": "to AND Smith"
    }
  }
}

可以得到两个结果,他们既在about字段包含 to单词,又在last_name字段中包含Smith单词。

参考

官方中文文档
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_document_oriented.html

https://blog.csdn.net/fanrenxiang/article/details/86477019

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Elasticsearch是一个基于Lucene的分布式开源搜索引擎,可用于快速、准确地存储、搜索和分析大量数据。它的基本概念和架构如下: 1. 索引(Index):类似于关系数据库中的“数据库”,是数据的逻辑容器。它由一个或多个分片(Shard)组成,分片可以分布在不同的节点上。 2. 分片(Shard):索引的物理单元,是Lucene的一个索引库(Index),它可以存储大量的数据。一个索引可以分成多个分片,每个分片可以存储一部分数据。 3. 节点(Node):Elasticsearch集群中的一个服务器实例,它是集群中的一员,负责存储数据和执行操作。 4. 集群(Cluster):由多个节点组成的分布式系统。每个集群有一个唯一的名称,节点可以通过名称加入到集群中。 5. 文档(Document):Elasticsearch中的一个基本单元,类似于关系数据库中的一条记录。每个文档都有一个唯一的ID,可以包含多个字段,字段可以存储不同类型的数据。 6. 映射(Mapping):定义文档的字段及其类型。每个索引都有一个映射,它定义了索引中文档的结构。 7. 查询(Query):用于搜索和过滤文档的条件。Elasticsearch支持多种类型的查询,包括全文查询、精确匹配、范围查询等。 8. 分析器(Analyzer):用于将文本数据分解为一系列词条,以便进行全文搜索。Elasticsearch支持多种分析器,包括标准分析器、中文分析器等。 以上是Elasticsearch基本概念和架构,了解这些概念对于使用Elasticsearch进行数据存储和搜索非常重要。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锥栗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值