栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch 增删改查

ElasticSearch 增删改查

目录

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

RESTful接URL的格式:添加查询修改删除 复杂查询

过滤查询(filter)range 范围过滤 RESTful接口URL的格式的增删改查

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

RESTful接URL的格式:
http://192.168.10.16:9200///[]

案例

添加
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
                    ]
                }
            }
        }
    }
}'
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/762078.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号