elasticsearch基础命令

1 字段分词分析:

复制代码
GET /store_info_data/_analyze
{
  "field": "storeName",
  "text":"20pilapala0"
}

2 精确查找,去除评分

复制代码
GET /store_info_data/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "price": "30"
        }
      }
    }
  }
}

3 组合查询案例

SELECT product FROM products

WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")

AND (price != 30)

复制代码
GET /store_info_data/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "price": 30
          }
        }
      ],
      "should": [
        {
          "term": {
            "price": 20
          }
        },
        {
          "match": {
            "productID": "XHDK-A-1293-#fJ3"
          }
        }
      ]
    }
  }
}

SELECT document FROM products

WHERE productID = "KDKE-B-9947-#kL5"

OR ( productID = "JODL-X-1937-#pV7" AND price = 30 )

复制代码
GET /store_info_data/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "productID":  "KDKE-B-9947-#kL5"
            
          }
        },
        {
          "bool": {
            "must": [
              {
                "match": {
                  "productID":  "JODL-X-1937-#pV7"
                  
                }
                
              },
              {
                "term": {
                  "price": {
                    "value": "30"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

4 范围查找

gt: > 大于(greater than)
lt: < 小于(less than)
gte: >= 大于或等于(greater than or equal to)
lte: <= 小于或等于(less than or equal to)

  • 如果我们想查找时间戳在过去一小时内的所有文档:

    "range" : {
    "timestamp" : {
    "gt" : "now-1h"
    }
    }

  • 日期计算还可以被应用到某个具体的时间,并非只能是一个像 now 这样的占位符。只要在某个日期后加上一个双管符号 (||) 并紧跟一个日期数学表达式就能做到:

    • 早于 2014 年 1 月 1 日加 1 月(2014 年 2 月 1 日 零时)

      "range" : {
      "timestamp" : {
      "gt" : "2014-01-01 00:00:00",
      "lt" : "2014-01-01 00:00:00||+1M"
      }
      }

4 exists(存在) & must_not + exists(不存在)

  • exists 用法 判断数据不为null

    GET store_info_data/_search
    {
    "query": {
    "constant_score": {
    "filter": {
    "exists": {
    "field": "author"
    }
    },
    "boost": 1.2
    }
    }
    }

  • must_not + exists用法

    GET store_info_data/_search
    {
    "query": {
    "bool": {
    "must_not": {
    "exists": {
    "field": "name"
    }
    }
    }
    }
    }

5 新增数据

复制代码
# POST
POST my-index-000001/_doc
{
  "my_float":   "2.0", 
  "my_integer": "3" 
}

# PUT 
PUT my-index-000001/_doc/2
{
  "my_float":   "2.0", 
  "my_integer": "3" 
}
相关推荐
【非典型Coder】40 分钟前
Statement和PreparedStatement区别
数据库
m0_736927042 小时前
想抓PostgreSQL里的慢SQL?pg_stat_statements基础黑匣子和pg_stat_monitor时间窗,谁能帮你更准揪出性能小偷?
java·数据库·sql·postgresql
lang201509282 小时前
MySQL 8.0.29 及以上版本中 SSL/TLS 会话复用(Session Reuse)
数据库·mysql
望获linux2 小时前
【实时Linux实战系列】使用 u-trace 或 a-trace 进行用户态应用剖析
java·linux·前端·网络·数据库·elasticsearch·操作系统
清和与九3 小时前
binLog、redoLog和undoLog的区别
数据库·oracle
望获linux3 小时前
【实时Linux实战系列】FPGA 与实时 Linux 的协同设计
大数据·linux·服务器·网络·数据库·fpga开发·操作系统
总有刁民想爱朕ha3 小时前
Python自动化从入门到实战(24)如何高效的备份mysql数据库,数据备份datadir目录直接复制可行吗?一篇给小白的完全指南
数据库·python·自动化·mysql数据库备份
朝九晚五ฺ3 小时前
【Redis学习】持久化机制(RDB/AOF)
数据库·redis·学习
虾说羊3 小时前
sql中连接方式
数据库·sql
liweiweili1263 小时前
Django中处理多数据库场景
数据库·python·django