栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElaticSearch 学习二(文档基本操作)

ElaticSearch 学习二(文档基本操作)

一、基本操作 1.创建索引以及数据

PUT /excel3/user/1
{
  "name":"谢月",
  "age":12,
  "desc":"她是一位小菜鸟",
  "tags":["开朗","热情","技术宅"]
}

PUT /excel3/user/2
{
  "name":"张三",
  "age":18,
  "desc":"法外狂徒",
  "tags":["活跃","渣男","旅游","爱交友"]
}

PUT /excel3/user/3
{
  "name":"里斯",
  "age":24,
  "desc":"嚣张跋扈",
  "tags":["阅读","帅哥","旅游","爱交友"]
}

2.修改数据

--第一种方式

PUT /excel3/user/1
{
  "name":"xieyue",
  "age":12,
  "desc":"她是一位小菜鸟",
  "tags":["开朗","热情","技术宅"]
}

--第二种方式,现在普遍采用的方式

POST /excel3/user/1/_update
{
  "doc":{
    "name":"xieyue22"
  }
}

3.删除数据

DELETE /excel3/user/1

删除之后excel索引下只有两条数据了

 4.查询数据

--查询固定文档id下的信息,类似于根据id查询

GET /excel3/user/2

--根据条件,在user类型文档下查询name中包含张三的信息

GET /excel3/user/_search?q=name:张三
 

es中的match查询参数都是类似于模糊查询,只要包含参数信息则满足条件,例如:

GET /excel3/user/_search?q=name:三,也是可以查询出name=张三的信息

二、复杂查询操作 1.单条件查询

--查询user中name包含里斯的信息,query 代表查询,match代表规则

GET /excel3/user/_search
{
  "query": {
    "match": {
      "name":"里斯"
    }
  }
}

【结果分析】

{
  "took" : 258,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,         
      "relation" : "eq"
    },
    "max_score" : 0.9983525,
    "hits" : [
      {
        "_index" : "excel3",
        "_type" : "user",
        "_id" : "3",
        "_score" : 0.9983525,
        "_source" : {
          "name" : "里斯",
          "age" : 24,
          "desc" : "嚣张跋扈",
          "tags" : [
            "阅读",
            "帅哥",
            "旅游",
            "爱交友"
          ]
        }
      },
      {
        "_index" : "excel3",
        "_type" : "user",
        "_id" : "4",
        "_score" : 0.8416345,
        "_source" : {
          "name" : "里斯_java",
          "age" : 28,
          "desc" : "为人和善",
          "tags" : [
            "理智",
            "清醒",
            "旅游",
            "爱交友"
          ]
        }
      }
    ]
  }

【hits】是我们比较重点关注的信息,下面说说这个标签下的信息:

total:查询结果数量;

hits:结果集,其中里面的_score代表权重,以后可以依据这个值进行排序,精确值越大的结果这个值越大。

2.多条件查询

数组类型字段多个参数查询,多个字段采用分号分割

GET /excel3/user/_search
{
  "query": {
    "match": {
      "tags":"技术 男"
    }
  }
}

must 相当于 and 

GET /excel3/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {"name": "里斯"}},
        {"match": {"age": 28}}
      ]
    }
  }
}

should 相当于  or

GET /excel3/user/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {"name": "里斯"}},
        {"match": {"age": 28}}
      ]
    }
  }
}

must_not 相当于  !=

GET /excel3/user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {"name": "里斯"}}
      ]
    }
  }
}

filter 过滤范围  年龄 在20-25之间的,gl 代表大于,lt代表小于,gte代表大于等于,lte代表小于等于

GET /excel3/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {"name": "里斯"}}
      ], 
      "filter": {"range": {
        "age": {
          "gte": 20,
          "lte": 25
        }
      }}
    }
  }
}

3.只查询部分字段   "_source": [需要查询的字段name、tags]

GET /excel3/user/_search
{
  "query": {
    "match": {
      "name":"里斯"
    }
  },
  "_source": ["name","tags"]
}

4.排序, 根据age 降序排序

GET /excel3/user/_search
{
  "query": {
    "match": {
      "name":"里斯"
    }
  },
  "sort": [{"age":{"order":"desc"}}]
}

5.分页查询,from 开始(从0开始),size 每页条数

GET /excel3/user/_search
{
  "query": {
    "match": {
      "name":"里斯"
    }
  },
  "sort": [{"age":{"order":"asc"}}], 
  "from": 0,
  "size": 2
}

6.精确查询,term,采用的就是倒排索引

term  倒排索引,效率较高

match 会使用分词器解析,先分析文档,再根据分析的文档进行查询

查询 name是里斯的数据

GET /excel3/user/_search
{
  "query": {
    "term": {
      "name":"里斯"
    }
  }
}

7.多个值精确查询,name中包含月的,desc中是她是一位小菜鸟

GET /excel4/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": 
          {"name":"月"}
        },
         {"term": 
          {"dessc":"她是一位小菜鸟"}
        }
      ]
    }
  }

8.高亮查询,并且自定义高亮条件

GET /excel4/_search
{
  "query":{
    "match": {
      "name":"谢月"
    }
  },
  "highlight": {
    "pre_tags": "

", 
    "post_tags": "

", 
    "fields": {
      "name":{}
    }
  }  
}

highlight 高亮显示属性

pre_tags 匹配值前缀

post_tags 匹配值后缀

三、字符类型分析text、keyword

俩个类型都是字符类型,但是若字段是keyword,说明该字段不会使用分词器解析。举例如下:

--1.创建索引规则,name为text字段类型、desc为keyword类型

PUT excel4
{
  "mappings": {
    "properties": {
      "name":{"type": "text"},
      "desc":{"type": "keyword"}                
    }
  }
}

--2.添加数据,3条

PUT /excel4/_doc/1
{
  "name":"谢月",
  "desc":"她是一位小菜鸟"
}

PUT /excel4/_doc/2
{
  "name":"张三",
  "desc":"她是一位小菜鸟哈哈"
}

PUT /excel4/_doc/3
{
  "name":"三月",
  "desc":"她是一位小菜鸟"
}

--3.精确查询desc是她是一位小菜鸟,结果只有id为1和3的数据

GET /excel4/_search
{
  "query":{
    "term": {
      "desc":"她是一位小菜鸟"
    }
  }
}

keyword类型查询时结果

 text类型查询时结果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735708.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号