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" 
}
相关推荐
Elastic 中国社区官方博客1 小时前
Elasticsearch:使用 LLM 实现传统搜索自动化
大数据·人工智能·elasticsearch·搜索引擎·ai·自动化·全文检索
cyt涛2 小时前
MyBatis 学习总结
数据库·sql·学习·mysql·mybatis·jdbc·lombok
Rookie也要加油2 小时前
01_SQLite
数据库·sqlite
liuxin334455662 小时前
教育技术革新:SpringBoot在线教育系统开发
数据库·spring boot·后端
看山还是山,看水还是。3 小时前
MySQL 管理
数据库·笔记·mysql·adb
fishmemory7sec3 小时前
Koa2项目实战2(路由管理、项目结构优化)
数据库·mongodb·koa
momo小菜pa3 小时前
【MySQL 09】表的内外连接
数据库·mysql
Jasonakeke3 小时前
【重学 MySQL】四十九、阿里 MySQL 命名规范及 MySQL8 DDL 的原子化
数据库·mysql
程序猿小D3 小时前
第二百六十九节 JPA教程 - JPA查询OrderBy两个属性示例
java·开发语言·数据库·windows·jpa
小宇成长录4 小时前
Mysql:数据库和表增删查改基本语句
数据库·mysql·数据库备份