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" 
}
相关推荐
时光追逐者2 分钟前
MongoDB从入门到实战之MongoDB快速入门(附带学习路线图)
数据库·学习·mongodb
头顶秃成一缕光25 分钟前
Redis的主从模式和哨兵模式
数据库·redis·缓存
AIGC大时代27 分钟前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
博睿谷IT99_28 分钟前
数据库证书可以选OCP认证吗?
数据库·oracle·开闭原则·ocp认证
乐维_lwops40 分钟前
数据库监控 | MongoDB监控全解析
数据库·mongodb·数据库监控
观无41 分钟前
Redis安装及入门应用
数据库·redis·缓存
柏油2 小时前
MySql InnoDB 事务实现之 undo log 日志
数据库·后端·mysql
Thomas_YXQ2 小时前
Unity3D IK解算器技术分析
开发语言·搜索引擎·unity·全文检索·unity3d·lucene
DolphinScheduler社区2 小时前
白鲸开源WhaleStudio与崖山数据库管理系统YashanDB完成产品兼容互认证
数据库·开源·认证·崖山数据库·白鲸开源
阑梦清川2 小时前
AI超级智能体项目教程(二)---后端项目初始化(设计knif4j接口文档的使用)
java·前端·数据库