ElasticSearch入门语法基础知识

1、创建测试索引

PUT /test_index_person
{
  "settings": {
    "analysis": {
      "analyzer": {
        "ik_analyzer": {
          "type": "custom",
          "tokenizer": "ik_smart"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "person_id": { "type": "integer" },
      "person_name": { 
        "type": "text",
        "analyzer": "ik_analyzer"
      },
      "gmt_create": { 
        "type": "date",
        "format": "strict_date_optional_time"
      },
      "gmt_modified": { 
        "type": "date",
        "format": "strict_date_optional_time"
      }
    }
  }
}

2、添加数据

POST /test_index_person/_doc
{
  "person_id": 1,
  "person_name": "张三 李四",
  "gmt_create": "2024-07-31T10:00:00",
  "gmt_modified": "2024-07-31T10:00:00"
}

POST /test_index_person/_doc
{
  "person_id": 2,
  "person_name": "王五 赵六",
  "gmt_create": "2024-07-31T11:00:00",
  "gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{
  "person_id": 3,
  "person_name": "葛哈哈哈张三",
  "gmt_create": "2024-07-31T11:00:00",
  "gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{
  "person_id": 4,
  "person_name": "葛哈哈哈小黑子",
  "gmt_create": "2024-07-31T11:00:00",
  "gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{
  "person_id": 5,
  "person_name": "赵六不是小黑子",
  "gmt_create": "2024-07-31T11:00:00",
  "gmt_modified": "2024-07-31T11:00:00"
}
POST /test_index_person/_doc
{
  "person_id": 6,
  "person_name": "张三是一个小黑子",
  "gmt_create": "2024-07-31T11:00:00",
  "gmt_modified": "2024-07-31T11:00:00"
}

3、修改数据

#更新数据,因为没有设置id,所以需要先查询到id
GET /test_index_person/_search
{
  "query": {
    "term": {
      "person_id": {
        "value": "1"
      }
    }
  }
}
POST /test_index_person/_update/VHvlB5EBxT0rb-io56C7
{
  "doc": {
    "person_name": "张三 李四哈哈哈"
  }
}

4、查询索引下的分词和ik分词器对搜索内容的分词

#查看test_index_person索引下对person_name某个内容的分词
GET /test_index_person/_analyze
{
  "field": "person_name",
  "text": "张三是一个小黑子"
}
# IK分词器对搜索内容的分词
GET /_analyze
{
  "analyzer": "ik_max_word", 
  "text": "张三"
}

5、查询所有文档

#匹配所有文档
GET /test_index_person/_search
{
  "query": {
    "match_all": {}
  }
}

6、分词查询测试

#查询分词测试
GET /test_index_person/_search
{
  "query": {
    "match": {
      "person_name": "葛赵六"
    }
  }
}

7、精确查询

#term查询适合用于匹配未分词的字段或者需要精确匹配的场景
GET /test_index_person/_search
{
  "query": {
    "term": {
      "person_id": {
        "value": "1"
      }
    }
  }
}

8、短语匹配

#短语匹配 返回所有person_name字段精确匹配短语"小黑子"的文档
GET /test_index_person/_search
{
  "query": {
    "match_phrase": {
      "person_name": "小黑子"
    }
  }
}

9、范围查询

#范围查询 gt(>) gte(>=)  lt(<) lte(<=) 
GET /test_index_person/_search
{
  "query": {
    "range": {
      "gmt_create": {
        "gte": "2024-01-31T11:00:00",
        "lt": "2024-07-31T11:00:00"
      }
    }
  }
}

10、模糊匹配

#模糊匹配
GET /test_index_person/_search
{
  "query": {
    "fuzzy": {
      "person_name": "张三"
    }
  }
}

11、前缀匹配查询(适用非分词字段)

#前缀匹配查询(适用非分词字段)
GET /test_index_person/_search
{
  "query": {
    "prefix": {
      "person_name": {
        "value": "张三"
      }
    }
  }
}

12、布尔查询

#布尔查询 必须匹配(must) 过滤(filter) 至少匹配一个(should)
GET /test_index_person/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "person_id": {
            "value": "1"
          }
        }}
      ],
      "filter": [
        {"range": {
          "gmt_create": {
            "gte": "2024-07-31T10:00:00"
          }
        }}
      ],
      "should": [
        {"term": {
          "person_name": {
            "value": "11"
          }
        }}
      ],
      "must_not": [
        {"term": {
          "person_name": {
            "value": "哈哈"
          }
        }}
      ]
    }
  }
}

13、分页

#分页 from开始 size 结束
GET /test_index_person/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1, 
  "size":  2
}

14、聚合查询

#对查询出来的值进行聚合查询 统计每个唯一值的个数
GET /test_index_person/_search
{
  "query": {
    "match": {
      "person_name": "张三"
    }
  }, 
  "aggs": {
    "test_aggs": {
      "terms": {
        "field": "gmt_create"
      }
    }
  }
}
#对查询出来的值进行聚合查询 统计每个唯一值的个数  仅仅返回聚合结果
GET /test_index_person/_search
{
  "size": 0, 
  "query": {
    "match": {
      "person_name": "张三"
    }
  }, 
  "aggs": {
    "test_aggs": {
      "terms": {
        "field": "gmt_create"
      }
    }
  }
}
#多个聚合
GET /test_index_person/_search
{
  "aggs": {
    "test_aggs": {
      "terms": {
        "field": "gmt_create"
      }
    },
    "test2_aggs":{
      "avg": {
        "field": "person_id"
      }
    }
  }
}
#子聚合 先分组,再计算
GET /test_index_person/_search
{
  "aggs": {
    "test_aggs": {
      "terms": {
        "field": "gmt_create"
      }, 
      "aggs": {
        "son_aggs_test": {
          "avg": {
            "field": "person_id"
          }
        }
      }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诸葛博仌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值