数据格式

Elasticsearch是面向文档型数据库,一条数据就是一个文档。
image.png
Elasticsearch 6.X中,一个index只能包含一个type,7.X中,Type概念已经被删除了

倒排索引

  • 正排索引

id content
——————-
1001 my name is zhangsan
1002 my name is lisi
通过id查询很快,但是如果需要查询content里的”name”,就需要模糊查询,每条都要遍历

  • 倒排索引

keyword id
—————-
name 1001, 1002
zhang 1001

索引操作

创建索引

对比关系型数据库,创建索引等同于创建数据库
在Postman中,向ES服务器发PUT请求:http://127.0.0.1:9200/shopping

  1. {
  2. "acknowledged": true,
  3. "shards_acknowledged": true,
  4. "index": "shopping"
  5. }

查询索引

GET请求 URL不变

  1. {
  2. "shopping": {
  3. "aliases": {},
  4. "mappings": {},
  5. "settings": {
  6. "index": {
  7. "routing": {
  8. "allocation": {
  9. "include": {
  10. "_tier_preference": "data_content"
  11. }
  12. }
  13. },
  14. "number_of_shards": "1",
  15. "provided_name": "shopping",
  16. "creation_date": "1650513030284",
  17. "number_of_replicas": "1",
  18. "uuid": "__fkgO0_R1ebjRJdflgxew",
  19. "version": {
  20. "created": "8010299"
  21. }
  22. }
  23. }
  24. }
  25. }

如果要查询所有索引
[http://localhost:9200/_cat/indices?v](http://localhost:9200/_cat/indices?v)

  1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  2. yellow open shopping __fkgO0_R1ebjRJdflgxew 1 1 0 0 225b 225b

删除索引

DELETE``http://127.0.0.1:9200/shopping

  1. {
  2. "acknowledged": true
  3. }

文档操作

创建数据

索引创建好后,创建文档,并添加数据。这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为JSON格式。
POST``http://127.0.0.1/shopping/_doc 必须要有body, _doc表示文档数据 ,也可以使用_create

  1. {
  2. "title":"小米手机",
  3. "category":"Xiaomi",
  4. "images":"xxx.jpg",
  5. "price":299
  6. }
  1. {
  2. "_index": "shopping",
  3. "_id": "hBXsSoAB2uyBztVe--TH",
  4. "_version": 1,
  5. "result": "created",
  6. "_shards": {
  7. "total": 2,
  8. "successful": 1,
  9. "failed": 0
  10. },
  11. "_seq_no": 0,
  12. "_primary_term": 1
  13. }

如果在uri中加入id,就不会自动生成随机的id了
[http://127.0.0.1/shopping/_doc](http://127.0.0.1/shopping/_doc)/1001

查询数据

GET``[http://localhost:9200/shopping/_doc/1001](http://localhost:9200/shopping/_doc/1001)

  1. {
  2. "_index": "shopping",
  3. "_id": "1001",
  4. "_version": 1,
  5. "_seq_no": 1,
  6. "_primary_term": 1,
  7. "found": true,
  8. "_source": {
  9. "title": "小米手机",
  10. "category": "Xiaomi",
  11. "images": "xxx.jpg",
  12. "price": 299
  13. }
  14. }

如果要获取索引下的所有数据
GET``[http://localhost:9200/shopping/](http://localhost:9200/shopping/_doc/1001)_search

  1. {
  2. "took": 746,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 2,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.0,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_id": "hBXsSoAB2uyBztVe--TH",
  20. "_score": 1.0,
  21. "_source": {
  22. "title": "小米手机",
  23. "category": "Xiaomi",
  24. "images": "xxx.jpg",
  25. "price": 299
  26. }
  27. },
  28. {
  29. "_index": "shopping",
  30. "_id": "1001",
  31. "_score": 1.0,
  32. "_source": {
  33. "title": "小米手机",
  34. "category": "Xiaomi",
  35. "images": "xxx.jpg",
  36. "price": 299
  37. }
  38. }
  39. ]
  40. }
  41. }

全量数据更新

PUT``ht[tp://localhost:9200/shopping/](http://localhost:9200/shopping/_doc/1001)_doc/1001

  1. {
  2. "title":"小米手机",
  3. "category":"Xiaomi",
  4. "images":"xxx.jpg",
  5. "price":199
  6. }
  1. {
  2. "_index": "shopping",
  3. "_id": "1001",
  4. "_version": 2,
  5. "result": "updated",
  6. "_shards": {
  7. "total": 2,
  8. "successful": 1,
  9. "failed": 0
  10. },
  11. "_seq_no": 2,
  12. "_primary_term": 1
  13. }

局部数据更新

POST``http://127.0.0.1:9200/shopping/_update/1001

  1. {
  2. "doc": {
  3. "title": "华为手机"
  4. }
  5. }
  1. {
  2. "_index": "shopping",
  3. "_id": "1001",
  4. "_version": 2,
  5. "result": "noop",
  6. "_shards": {
  7. "total": 0,
  8. "successful": 0,
  9. "failed": 0
  10. },
  11. "_seq_no": 2,
  12. "_primary_term": 1
  13. }

删除数据

DELETE``http://127.0.0.1:9200/shopping/_doc/1001

条件查询

通过路径加参数查询

GET``http://127.0.0.1/shopping/_search?q=category:Xiaomi

通过请求体查询

GET``[http://127.0.0.1/shopping/_search](http://127.0.0.1/shopping/_search)

  1. {
  2. "query" : {
  3. "match" : {
  4. "category" : "Xiaomi"
  5. }
  6. }
  7. }
  1. {
  2. "query" : {
  3. "match_all" : {
  4. }
  5. }
  6. }

分页查询&查询排序

GET``[http://127.0.0.1/shopping/_search](http://127.0.0.1/shopping/_search)

  • from 从哪一条数据开始 第一条数据为0
  • size 每页几条数据
  • _source 指定查询返回的字段
  • sort 排序 order: asc desc

从起始位置0开始(第一条数据),每页2条数据,只保留title数据, 按照价格降序排序

  1. {
  2. "query" : {
  3. "match_all" : {
  4. }
  5. },
  6. "from": 0,
  7. "size": 2,
  8. "_source" : ["title"],
  9. "sort" : {
  10. "price" : {
  11. "order" : "desc"
  12. }
  13. }
  14. }
  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 3,
  13. "relation": "eq"
  14. },
  15. "max_score": 1.0,
  16. "hits": [
  17. {
  18. "_index": "shopping",
  19. "_id": "hBXsSoAB2uyBztVe--TH",
  20. "_score": 1.0,
  21. "_source": {
  22. "title": "小米手机",
  23. "category": "Xiaomi",
  24. "images": "xxx.jpg",
  25. "price": 299
  26. }
  27. },
  28. {
  29. "_index": "shopping",
  30. "_id": "1001",
  31. "_score": 1.0,
  32. "_source": {
  33. "title": "小米手机",
  34. "category": "Xiaomi",
  35. "images": "xxx.jpg",
  36. "price": 199
  37. }
  38. }
  39. ]
  40. }
  41. }

多条件查询

GET``[http://127.0.0.1/shopping/_search](http://127.0.0.1/shopping/_search)

  • bool
  • must

    1. {
    2. "query":{
    3. "bool":{
    4. "must":[
    5. {
    6. "match":{
    7. "category":"Xiaomi"
    8. }
    9. },
    10. {
    11. "match":{
    12. "price":199
    13. }
    14. }
    15. ]
    16. }
    17. }
    18. }
  • should

    1. {
    2. "query":{
    3. "bool":{
    4. "should":[
    5. {
    6. "match":{
    7. "category":"Xiaomi"
    8. }
    9. },
    10. {
    11. "match":{
    12. "category":"Huawei"
    13. }
    14. }
    15. ]
    16. }
    17. }
    18. }

    范围查询

    GET``[http://127.0.0.1/shopping/_search](http://127.0.0.1/shopping/_search)

  • filter

  • range
  • gt&lt 大于 小于
    1. {
    2. "query":{
    3. "bool":{
    4. "filter":{
    5. "range":{
    6. "price":{
    7. "gt":199,
    8. "lt":300
    9. }
    10. }
    11. }
    12. }
    13. }
    14. }

    全文检索

    当保存文档数据时 ES会对文档数据进行拆解,保存到倒排索引中。当通过ES进行查询,也会通过倒排索引进行匹配。因此,即使只查询”category”:”Xiao”也会查到
    1. {
    2. "query" : {
    3. "match" : {
    4. "title" : "小米"
    5. }
    6. }
    7. }
    如果查询”title”=”小华” 会既查到小米也查到华为的数据

    完全匹配

    1. {
    2. "query" : {
    3. "match_phrase" : {
    4. "title" : "小"
    5. }
    6. }
    7. }
    这样就查不到了

    高亮查询

    1. {
    2. "query" : {
    3. "match_phrase" : {
    4. "title" : "小"
    5. }
    6. },
    7. "highlight" : {
    8. "fields" : {
    9. "category" : {}
    10. }

    聚合操作

    1. {
    2. "aggs" : { // 聚合操作
    3. "price_group" : { // 名称 随意起的
    4. "terms" : { //
    5. "field" : "price" // 分组字段
    6. }
    7. }
    8. },
    9. "size":0
    10. }
    size:0过滤掉了查询的原始数据
    1. {
    2. "aggs" : { // 聚合操作
    3. "price_avg" : { // 名称 随意起的
    4. "avg" : { //
    5. "field" : "price" // 分组字段
    6. }
    7. }
    8. },
    9. "size":0
    10. }

    映射关系

    其实就是表的结构信息
  1. 先创建user索引
  2. 创建user的映射关系

PUT``[http://localhost:9200/user/_mapping](http://localhost:9200/user/_mapping)

  1. {
  2. "properties" : {
  3. "name" : {
  4. "type" : "text",
  5. "index" : true
  6. },
  7. "sex" : {
  8. "type" : "keyword",
  9. "index" : true
  10. },
  11. "tel" : {
  12. "type" : "keyword",
  13. "index" : false
  14. }
  15. }
  16. }
  1. 插入数据

PUT``[http://localhost:9200/user/_create/001](http://localhost:9200/user/_create/001)

  1. {
  2. "name" : "汤姆",
  3. "sex" : "男的",
  4. "tel" : "12345"
  5. }
  1. 查询

GET``[http://localhost:9200/user/_search](http://localhost:9200/user/_search)

  1. {
  2. "query" : {
  3. "match" : {
  4. "name" : "汤"
  5. }
  6. }
  7. }
  1. {
  2. "query" : {
  3. "match" : {
  4. "sex" : "男"
  5. }
  6. }
  7. }

因为sex字段是”keyword”类型,而name是”text”类型

  1. {
  2. "query" : {
  3. "match" : {
  4. "tel" : "12345"
  5. }
  6. }
  7. }

因为字段的index设置为了false