ES 基础语法 CRUD

以下语法是在ES7中测试的,低版本可能会有不兼容的问题

创建索引

PUT /article
{
  "mappings": {
    "properties": {
      "title":{
        "type": "text",
        "analyzer": "english"
      }
    }
  }
}

给索引添加字段

PUT /article/_mapping
{
    "properties": {
      "content":{
        "type": "text",
        "analyzer": "standard"
      }
    }
}

老版本中在请求路径上可能还会添加_doc 例如: PUT /article/_mapping
这个新版本中添加会报错。

添加数据

POST article/_doc/1
{
  "title":" a dog"
}

_doc: 是固定写法

查询所有

GET /article/_search

查询指定ID

GET article/_doc/1

指定ID全量修改


PUT article/_doc/1
{
  "title":"a cat"
}

指定id部分字断修改

POST article/_update/1
{
  "doc": {
    "title":" a bird"
  }
}

指定_create防止重复创建

POST article/_create/1
{
  "title":"哈哈"
}

QSL_查询

match 查询

GET /article/_search
{
  "query": {
    "match": {
      "title": "dog"
    }
  }
}

结果
在这里插入图片描述
title 等于a dog 和big dog都查询出来了

GET /article/_search
{
  "query": {
    "match": {
      "title": "a dog"
    }
  }
}

将查询条件 改成a dog 依然把两个都查询出来了

这里为什么也把 title等于a dog查询出来是因为在查询之前会把 a dog 进行拆分 出现一个 'a' 一个'dog' 两个词进行查询 因此就查询出来了两个

term查询

term查询,不进行词的分析,直接去索引查询,及搜索关键词和索引内词的精确匹配

GET /article/_search
{
  "query": {
    "term": {
      "title": {
        "value": "big dog"
      }
    }
  }
}

结果
在这里插入图片描述
可以看到结果是什么也没有。
因为es 是安装索引查询的, 不是查询的内容。 在建立索引的时候,通过分词器的索引是两个 bigdog 而term是完全匹配,是不把关键字进行分词, 所以就是相当于拿 big dog 去匹配bigdog 因此就匹配不上。

Bool 加filter 查询

GET /article/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "red dog"
        }
        }
      ], 
      "filter": [
        {"term": {
          "content": "red"
        }}
      ]
    }
  }
}

filter 过滤是没有得分的,match查询是有得分的。 这是结合在一起,得分是按照match中字段的得分来给的。

例二:

{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "title":"Search"
                    }
                },
                {
                    "match":{
                        "tagline":"Elasticsearch"
                    }
                }
            ],
            "filter":[
                {
                    "term":{
                        "title":"steve"
                    }
                },
                {
                    "term":{
                        "cast.name":"gaspard"
                    }
                },
                {
                    "range":{
                        "release_date":{
                            "lte":"2015/01/01"
                        }
                    }
                },
                {
                    "range":{
                        "popularity":{
                            "gte":"25"
                        }
                    }
                }
            ]
        }
    }
}

交个朋友吧

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值