关于文档的基本操作---ElasticSearch

关于文档的基本操作(重点)

基本操作

添加数据

PUT /psz/user/1
{
    
    
  "name": "psz",
  "age": 22,
  "desc": "偶像派程序员",
  "tags": ["暖","帅"]
}

获取数据

GEt psz/user/1
===============输出===========
{
    
    
  "_index" : "psz",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    
    
    "name" : "psz",
    "age" : 22,
    "desc" : "偶像派程序员",
    "tags" : [
      "暖",
      "帅"
    ]
  }
}

更新数据PUT

img

更新数据,推荐POST _update

  • 不推荐
POST psz/user/1
{
    
    
  "doc":{
    
    
    "name": "庞庞胖"    #后面信息会没有
  }
}
  • 推荐!
POST psz/user/1/_update
{
    
    
  "doc":{
    
    
    "name": "庞庞胖"    #后面信息存在
  }
}

简单搜索 GET

GET psz/user/1

简答的条件查询:根据默认映射规则产生基本的查询

GET psz/user/_search?q=name:庞世宗

复杂查询

查询,参数使用JSON体

GET psz/user/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "name": "庞世宗"   //根据name匹配
    }  
  },
    "_source": ["name","age"],  //结果的过滤,只显示name和age
    "sort": [
    {
    
    
      "age": {
    
    
        "order": "desc" //根据年龄降序
    }
    }
  ],
    
  "from": 0, //分页:起始值,从0还是
  "size": 1  //返回多少条数据
}
  • 之后只用java操作es时候,所有的对象和方法就是这里面的key
  • 分页前端 /search/{current}/{pagesize}

布尔值查询

must(对应mysql中的and) ,所有条件都要符合

GET psz/user/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [  //相当于and
        {
    
    
          "match": {
    
    
            "name": "庞世宗"
          }
          
        },
        {
    
    
          "match": {
    
    
            "age": 22
          }
        }
          
      ]
    }
  }
}

shoule(对应mysql中的or)

GET psz/user/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "should": [ //should相当于or
        {
    
    
          "match": {
    
    
            "name": "庞世宗"
          }
          
        },
        {
    
    
          "match": {
    
    
            "age": 22
          }
        }
          
      ]
    }
  }
}

must_not (对应mysql中的not)

过滤器

GET psz/user/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "should": [
        {
    
    
          "match": {
    
    
            "name": "庞世宗"
          }
          
        }
      ],
      "filter": [
        {
    
    
          "range": {
    
    
            "age": {
    
    
              "gt": 20   //过滤年龄大于20的
            }
          }
        }
      ]
    }
  }
}

多条件查询

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bmL2gesJ-1610955698825)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20210117233812605.png)]

精确查询

  • trem查询是直接通过倒排索引指定的词条进行精确的查找的。

关于分词:

trem,直接查询精确地

match,会使用分词器解析

关于类型:

text: 分词器会解析

keywords: 不会被拆分

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rTBEDylJ-1610955698826)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20210117234310173.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YUZ64FCL-1610955698827)(C:\Users\王东梁\AppData\Roaming\Typora\typora-user-images\image-20210117234442418.png)]

扫描二维码关注公众号,回复: 12592076 查看本文章

高亮查询

GET psz/user/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "name": "庞世宗"
    }
  },
  "_source": ["name","age"],
  "sort": [
    {
    
    
      "age": {
    
    
        "order": "desc"
      }
    }
  ],
  "highlight": //高亮
  {
    
    
    "pre_tags": "<P>",   //自定义高亮
    "post_tags": "</P>", 
    "fields": {
    
    
      "name":{
    
    }  //自定义高亮区域
    }  
  }
}

猜你喜欢

转载自blog.csdn.net/qq_45783660/article/details/112785079