创建索引等同于创建数据库
创建一个shopping的索引需要 在Postman中,向ES服务器发送 PUT
请求:
http://127.0.0.1:9200/shopping
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "shopping"
}
PUT
具有幂等性再次请求相同名称的索引会出现问题,提示你重复了。
获取 shopping 索引相关信息。
用 GET
请求发送
http://127.0.0.1:9200/shopping
{
"shopping": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "shopping",
"creation_date": "1670935714623",
"number_of_replicas": "1",
"uuid": "vC5bzEg2SK-yTYK20D2jMA",
"version": {
"created": "7160299"
}
}
}
}
}
GET
http://127.0.0.1:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .geoip_databases 2iBY2CVPTaSv3xUEjYU_2g 1 0 40 0 38mb 38mb
yellow open shopping vC5bzEg2SK-yTYK20D2jMA 1 1 0 0 226b 226b
DELETE
http://127.0.0.1:9200/shopping
{
"acknowledged": true
}
POST
http://127.0.0.1:9200/shopping/_doc/1001 并且传递json参数
{
"title":"小米手机",
"category":"小米",
"images":"1.png",
"price":1999
}
_doc 文档数据
/1001 指定id
id可以不填发送 http://127.0.0.1:9200/shopping/_doc 会随机创建id
返回json
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
GET
http://127.0.0.1:9200/shopping/_doc/1001 无需携带body
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 1,
"_seq_no": 1,
"_primary_term": 1,
"found": true,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "1.png",
"price": 1999
}
}
GET
http://127.0.0.1:9200/shopping/_search 无需携带body
{
"took": 173,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "nveSC4UBUGCCAe-4B2he",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "1.png",
"price": 1999
}
},
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_score": 1.0,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "1.png",
"price": 1999
}
}
]
}
}
PUT
http://127.0.0.1:9200/shopping/_doc/1001
{
"title":"小米手机",
"category":"小米",
"images":"1.png",
"price":4999
}
返回json
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
POST
http://127.0.0.1:9200/shopping/_update/1001
{
"doc":{
"title":"华为手机"
}
}
返回json
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
DELETE
http://127.0.0.1:9200/shopping/_doc/1001
{
"_index": "shopping",
"_type": "_doc",
"_id": "1001",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 1
}
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match":{
"category":"小米"
}
}
}
返回json
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.26706278,
"hits": [
{
"_index": "shopping",
"_type": "_doc",
"_id": "nveSC4UBUGCCAe-4B2he",
"_score": 0.26706278,
"_source": {
"title": "小米手机",
"category": "小米",
"images": "1.png",
"price": 1999
}
}
]
}
}
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_all":{
}
}
}
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_all":{
}
},
"from":0, // 偏移量
"size":2 // 每页数量
}
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_all":{
}
},
"from":0, // 偏移量
"size":2, // 每页数量
"_source":["title"]
}
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_all":{
}
},
"from":0, // 偏移量
"size":2, // 每页数量
"_source":["title"],
"sort":{
"price":{
"order":"desc" // asc
}
}
}
同时查询品牌是小米,价格为1999的手机
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"bool":{
"must":[ // 多个条件同时成立(and) 还有 should(or)
{
"match":{
"category":"小米"
}
},
{
"match":{
"price":1999
}
}
]
}
}
}
查询类型是小米或者华为的价格大于 1000 的手机
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"category":"小米"
}
},
{
"match":{
"category":"华为"
}
}
],
"filter":{
"range":{
"price":{
"gt":1000
}
}
}
}
}
}
我们之前使用都是 全文检索匹配
输入的内容会被分词查询。如果想要其精准匹配需要使用 完全匹配
match_phrase
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_phrase":{
"category":"小米"
}
}
}
对查询出来结果的数据 category
进行高亮显示
GET
http://127.0.0.1:9200/shopping/_search
{
"query":{
"match_phrase":{
"category":"小米"
}
},
"highlight":{
"fields":{
"category":{}
}
}
}
对价格进行分组
GET
http://127.0.0.1:9200/shopping/_search
{
"aggs":{ // 聚合操作
"price_group":{ // 分组后名称,随意起
"terms":{ // 分组
"field":"price" // 分组字段
}
}
}
}
对价格进行平均
GET
http://127.0.0.1:9200/shopping/_search
{
"aggs":{ // 聚合操作
"price_avg":{ // 分组后名称,随意起
"avg":{ // 分组
"field":"price" // 分组字段
}
}
}
}