ElasticSearch 增删改查

35 篇文章 0 订阅

目录

RESTful接口URL的格式的增删改查

通过http形式发送请求对es进行操作

image-20220310145553887

RESTful接URL的格式:

http://192.168.10.16:9200/<index>/<type>/[<id>]

案例

添加

curl -XPUT 'http://118.178.242.230:9200/store/books/1' -d '{
	"title" : "Elasticseach:The Definitive Guide",
	"name" : {
		"first" : "Zachary",
		"last" : "Tong"
	},
	"publish_date" : "2015-02-06",
	"price" : "400.0"
}'

查询

curl -XGET "http://118.178.242.230:9200/store/books/1"

浏览器访问:http://118.178.242.230:9200/store/books/1

curl -XPUT 'http://118.178.242.230:9200/store/books/4' -d '{
	"title" : "Elasticseach2:The Definitive Guide",
	"name" : {
		"first" : "Guide
		"last" : "Guide
	},
	"publish_date" : "2015-02-07",
	"price" : "50.0"
}'

_source代表返回的字段

curl -XGET "http://118.178.242.230:9200/store/books/1?_source=title"

curl -XGET "http://118.178.242.230:9200/store/books/1?_source=title,name"

修改

覆盖方式

curl -XPUT 'http://118.178.242.230:9200/store/books/3' -d '{
	"title" : "Elasticseach2:The Definitive Guide",
	"name" : {
		"first" : "Zacharyaa",
		"last" : "Tongaa"
	},
	"publish_date" : "2015-02-07",
	"price" : "999.0"
}'

通过_update API的方式单独更新你想要更新的

curl -XPOST 'http://118.178.242.230:9200/store/books/2/_update' -d '{
	"doc" : {
		"price" : 8888.0
	}
}'

删除

curl -XDELETE 'http://118.178.242.230:9200/store/books/1'

复杂查询

过滤查询(filter)

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"must" : {
				"match_all" : {}
			},
			"filter" : {
				"term" : {
					"price" : "400.0"
				}
			}
		}
	}
}'

_search : 查询

query : 查询条件

bool : 组合查询

term : 部分词

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"constant_score" : {
			"filter" : {
				"term" : {
					"price" : "400.0"
				}
			}
		}
	}
}'

查询price = “400.0”

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"filter" : {
				"term" : {
					"price" : "400.0"
				}
			}
		}
	}
}'

查询price = “400.0” 或者 “50.0”

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"filter" : {
				"terms" : {
					"price" : ["400.0","50.0"]
				}
			}
		}
	}
}'

查询时间 = “2015-02-06”

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"filter" : {
				"term": {
				 	"publish_date" : "2015-02-06"
				}
			}
		}
	}
}'

查询select * from books where (price = 50.0 or prcie = 400.0) and publish_date != “2015-02-06”

must : 条件必须满足,相当于and

should : 条件可以满足也可以不满足,相当于or

must_not : 条件不需要满足 , 相当于 not

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"should" : [
				{"term" : {"price" : 50.0}},
				{"term" : {"price" : 400.0}}
			],
			"must_not" : {
				"term" : {
					"publish_date" : "2015-02-06"
				}
			}
		}
	}
}'

select * from books where price = 50.0 or (publish_date = “2015-02-06” and price = 400.0)

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
		"bool" : {
			"should" : [
			{	
				"term" : {
					"price" : 50.0
				}
			},
			{
				"bool" : {
					"must" : [
						{
							"term" : {
								"publish_date" : "2015-02-06"
							}
						},
						{
							"term": {
								"price": 400.0
								}
							}
						]
					}
				}
			]
		}
	}
}'

range 范围过滤

select * friom books where price >= 10 and price < 99

  • gt : > 大于

  • lt : < 小于

  • gte : >= 大于等于

  • lte : <= 小于等于

    curl -XGET ‘http://118.178.242.230:9200/store/books/_search’ -d ‘{
    “query” : {
    “range” : {
    “price” : {
    “gte” : 10,
    “lt” : 99
    }
    }
    }
    }’

注意:如果没有定义类型对比的是字符串

name和author都必须包含Guide,并且价钱等于50.0或者400.0

curl -XGET 'http://118.178.242.230:9200/store/books/_search' -d '{
	"query" : {
        "bool" : {
            "must" : {
                "multi_match" : {
                    "operator" : "and",
                    "fields" : [
                        "name",
                        "author"
                    ],
                    "query" : "Guide"
                }
            },
            "filter" : {
                "terms" : {
                    "price" : [
                        50.0,
                        400.0
                    ]
                }
            }
        }
    }
}'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值