Elasticsearch 基本用法
1.查询ES状态
curl 192.168.0.234:9200
2.新建索引数据
# 新建索引test_index 类型为test id为1 数据为 {} 内数据
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/1" -d'
{
"title": "这是一条测试数据",
"msg": "测试数据巴拉啦",
"year": 2018
}'
3.查询数据
curl -H "Content-Type: application/json" -XGET "http://192.168.0.234:9200/test_index/test/1"
4.更新数据(添加一个字段数据)
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/1" -d'
{
"title": "这是一条测试数据",
"director": "测试数据巴拉啦",
"year": 2018,
"test_list": ["test111", "test222"]
}'
5.删除数据
curl -H "Content-Type: application/json" -XDELETE "http://192.168.0.234:9200/test_index/test/1"
6.添加多条测试数据
# 添加测试数据,学习查询
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/1" -d'
{
"title": "这是一条测试数据111",
"msg": "测试数据巴拉啦",
"year": 2018,
"test_list": ["test111", "test222", "test333"]
}'
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/2" -d'
{
"title": "这是一条测试数据222",
"msg": "测试数据巴拉啦",
"year": 2019,
"test_list": ["test111", "test222"]
}'
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/3" -d'
{
"title": "这是一条测试数据333",
"msg": "测试数据巴拉啦",
"year": 2018,
"test_list": ["test111", "test222", "test333"]
}'
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/4" -d'
{
"title": "这是一条测试数据444",
"msg": "测试数据巴拉啦",
"year": 2019,
"test_list": ["test111", "test222", "test333", "test444", "test555"]
}'
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/5" -d'
{
"title": "这是一条测试数据444",
"msg": "测试数据巴拉啦",
"year": 2020,
"test_list": ["test111", "test222", "test333", "test444"]
}'
curl -H "Content-Type: application/json" -XPUT "http://192.168.0.234:9200/test_index/test/6" -d'
{
"title": "这是一条测试test444",
"msg": "测试数据巴拉啦",
"year": 2020,
"test_list": ["test111", "test222", "test333"]
}'
6.1全文检索带有test555的数据
curl -H "Content-Type: application/json" -XPOST "http://192.168.0.234:9200/_search" -d'
{
"query": {
"query_string": {
"query": "test555"
}
}
}'
6.2查询带year等于2020的数据(获取到两条数据)
curl -H "Content-Type: application/json" -XPOST "http://192.168.0.234:9200/_search" -d'
{
"query":{
"bool":{
"filter":{
"term":{
"year":2020
}
}
}
}
}'
6.3指定字段搜索
# 搜索带有test444的数据
curl -H "Content-Type: application/json" -XPOST "http://192.168.0.234:9200/_search" -d'
{
"query": {
"query_string": {
"query": "test444"
}
}
}'
# 搜索带有test444的数据,并指定title字段
curl -H "Content-Type: application/json" -XPOST "http://192.168.0.234:9200/_search" -d'
{
"query": {
"query_string": {
"query": "test444",
"fields": ["title"]
}
}
}'