博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ElasticSearch 基本操作
阅读量:4681 次
发布时间:2019-06-09

本文共 5712 字,大约阅读时间需要 19 分钟。

ElasticSearch 基本操作

ES中最基本的增删改查操作,这里做个汇总,以后可以看一下。查找部分还有很多没有写出来,内容是在太多了。

1. 增加索引

增加索引my_store,字段有price,productID

curl -X PUT "localhost:9200/my_store" -H 'Content-Type: application/json' -d'{    "settings": {        "number_of_shards": 3,        "number_of_replicas": 1    }    "mappings" : {        "products" : {            "properties" : {                "price": {                    "type": "keyword"                },                "productID" : {                    "type" : "keyword"                }            }        }    }}'

2. 增加文档

指定ID

curl -X PUT "localhost:9200/my_store/products/1" -H 'Content-Type: application/json' -d'{    "price": 20,    "productID": "XHDK-A-1293-#fJ3"}'

不指定ID

curl -X POST "localhost:9200/my_store/products/" -H 'Content-Type: application/json' -d'{    "price": 20,    "productID": "XHDK-A-1293-#fJ3"}'

1. 删除文档

curl -X DELETE "localhost:9200/my_store/products/1"

2. 删除索引

curl -X DELETE "localhost:9200/my_store"

修改id为1的文档的 price 为 50

curl -X POST "localhost:9200/my_store/products/1/_update" -H 'Content-Type: application/json' -d'{    "docs": {        "price": 50    }}'

这里只是粗略介绍

1. 通过 _id 查询

curl -X GET "localhost:9200/my_store/products/1"

2. 条件查询

查询该索引下所有的记录

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "match_all": {}    }}'

限制条数(从第一条开始,显示一条)

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "match_all": {}    },    "from": 1,    "size": 1}'

限定条件

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "match": {            "price": 30        }    }}'

设定升降序

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "match": {            "price": 30        }    },    "sort": [        {"productID": {"order": "desc"}}    ]}'

3. 聚合查询

查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "aggs": {        "group_by_word_count": {            "terms": {                "field": "word_count"            }        },        "group_by_publish_date": {            "terms": {                "field": "publish_date"            }        }    }}'

计算

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "aggs": {        "grades_word_count": {            "stats": {                "field": "word_count"            }        }    }}'

高级查询

  • 子条件查询 特定字段查询所指特定值

    • Query context

      在查询过程中,除了判断文档是否满足查询条件外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配的有多好。

      • 全文本查询 针对文本类型数据
      • 字段级别查询 针对结构化数据(数字,日期等)
    • Filter context

      在查询过程中,指判断该文档是否满足条件,只有 YES 或 NO。

  • 符合条件查询 以一定的逻辑组合子条件查询

1. query

1.1. 模糊匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "match": {            "price": 30        }    }}'

缺陷:查询组合词的时候会被自动拆分,比如说ElasticSearch入门会被拆分为ElasticSearch入门

1.2. 习语匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "match_phrase": {            "title": "ElasticSearch入门"        }    }}'

1.3. 多字段匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "multi_match": {            "query": "ElasticSearch",            "fields": ["title", "content"]        }    }}'

1.4. 语法匹配 - 非结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "query_string": {            "query": "ElasticSearch AND 入门"        }    }}'
curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "query_string": {            "query": "(ElasticSearch AND 入门) OR Python"        }    }}'
curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "query_string": {            "query": "ElasticSearch OR 入门"            "fields": ["title", "content"]        }    }}'

1.5. 字段查询 - 结构化数据查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "term": {            "title": "ElasticSearch"        }    }}'

2. filter

和 query 最大的区别在于没有_score了,并且会对结果进行缓存。

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "bool": {            "filter": {                "term": {                    "price": 30                }            }        }    }}'

3. 复合查询

3.1. 固定分数查询

curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{    "query": {        "constant_score": {            "filter": {                "match": {                    "title": "ElasticSearch"                }            },            "boost": 2        }    }}'

3.2. 布尔查询

  • should:相当于sql中的 or

    curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{  "query": {        "bool": {            "should": [                {"match": {"title": "ElasticSearch"}},                {"match": {"content": "ES"}}            ]        }  }}'
  • must:相当于sql中的 is

    curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{  "query": {        "bool": {            "must": [                {"match": {"title": "ElasticSearch"}},                {"match": {"content": "ES"}}            ]        }  }}'
  • must_not:相当于sql中的 is not

    curl -X POST "localhost:9200/my_store/_search" -H "Content-Type:application/json" -d '{  "query": {        "bool": {            "must_not": {                "term": {                    "title": "ElasticSearch"                }            }        }  }}'

转载于:https://www.cnblogs.com/yisany/p/10251829.html

你可能感兴趣的文章
SAS数据挖掘实战篇【一】
查看>>
SAS市场研究应用介绍:组合/联合分析
查看>>
python urllib urllib2
查看>>
L1-056 猜数字
查看>>
spring boot启动原理步骤分析
查看>>
Reverse Integer 旋转数字
查看>>
如何查看linux系统下的各种日志文件 linux 系统日志的分析大全
查看>>
MySQL中间件Atlas安装及使用
查看>>
LCA最近公共祖先-- HDU 2586
查看>>
IOS工作笔记(八)
查看>>
算法——插入排序
查看>>
VM上安装苹果虚拟机
查看>>
RHEL6 双网卡绑定
查看>>
mysqld 已死,但是 subsys 被锁
查看>>
Anaconda安装与常用命令及方法(深度学习入门1)
查看>>
学习笔记4-Action参数绑定
查看>>
linux下怎样批量更改文件后缀名
查看>>
Luogu 3119 [USACO15JAN]草鉴定Grass Cownoisseur
查看>>
js 中prototype运用(数组)
查看>>
ORA-01439: column to be modified must be empty to change datatype
查看>>