Class 3 基于ECS构建微信公众号管理系统

时间:2020-08-08 20:07:43   收藏:0   阅读:107
1、es是基于lucene开发的一个分布式全文索引框架。往ES中存储和从ES中查询,格式为Json
索引:Index 相当于数据库中的database
类型:type 相当于数据库中的table
主键:id 相当于数据库中的主键

往ES中存储数据,其实就是往ES中的index下的type存储Json数据

RESTFul风格API
通过http的形式,发送请求,对ES进行操作
查询: GET
删除: DELETE
添加: PUT/POST
修改: PUT/POST
 

1、PUT操作,store为database;books为table;2为主键

curl -XPUT http://hadoop100:9200/store/books/2 -d {
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}

2、GET操作

a)根据id查询

curl -XGET http://hadoop100:9200/store/books/1

b)通过_source获取指定的字段

curl -XGET http://hadoop100:9200/store/books/1?_source=title
curl -XGET http://hadoop100:9200/store/books/1?_source=title,price
curl -XGET http://hadoop100:9200/store/books/1?_source

c)最简单_search 结合filter查询

curl -XGET http://hadoop100:9200/store/books/_search -d {
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "term": {
          "price": 35.99
        }
      }
    }
  }
}
#指定多个值
curl -XGET http://hadoop100:9200/store/books/_search -d {
    "query" : {
        "bool" : {
            "filter" : {
                "terms" : {
                    "price" : [35.99, 99.99]
                  }
              }
        }
    }
}
curl -XGET http://hadoop100:9200/store/books/_search -d {
  "query" : {
    "bool" : {
        "filter" : {
           "term" : {
              "publish_date" : "2015-02-06"
            }
          }
      }
  }
}

d)bool过滤查询,可以做组合过滤查询

# SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND publish_date != "2016-02-06"
# 类似的,Elasticsearch也有 and, or, not这样的组合条件的查询方式
# 格式如下:
#  {
#    "bool" : {
#    "must" :     [],
#    "should" :   [],
#    "must_not" : [],
#    }
#  }
#
# must: 条件必须满足,相当于 and
# should: 条件可以满足也可以不满足,相当于 or
# must_not: 条件不需要满足,相当于 not

curl -XGET http://hadoop100:9200/store/books/_search -d {
  "query" : {
    "bool" : {
      "should" : [
        { "term" : {"price" : 35.99}},
        { "term" : {"price" : 99.99}}
      ],
      "must_not" : {
        "term" : {"publish_date" : "2016-02-06"}
      }
    }
  }
}

e)嵌套查询

# SELECT * FROM books WHERE price = 35.99 OR ( publish_date = "2016-02-06" AND price = 99.99 )

curl -XGET http://hadoop100:9200/store/books/_search -d {
    "query": {
        "bool": {
            "should": [
                {
                    "term": {
                        "price": 35.99
                    }
                },
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "publish_date": "2016-02-06"
                                }
                            },
                            {
                                "term": {
                                    "price": 99.99
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

f)range范围过滤

# SELECT * FROM books WHERE price >= 10 AND price < 99
# gt :  > 大于
# lt :  < 小于
# gte :  >= 大于等于
# lte :  <= 小于等于

curl -XGET http://hadoop100:9200/store/books/_search -d {
    "query": {
        "range" : {
            "price" : {
                "gte" : 10,
                "lt" : 99
            }
        }
    }
}
#name和author都必须包含Guide,并且价钱等于33.99或者188.99
curl -XGET http://hadoop100:9200/store/books/_search -d {
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "operator": "and",
                    "fields": [
                        "name",
                        "author"
                    ],
                    "query": "Guide"
                }
            },
            "filter": {
                "terms": {
                    "price": [
                        35.99,
                        188.99
                    ]
                }
            }
        }
    }
}

3、UPDATE操作

a)以通过覆盖的方式更新

curl -XPUT http://192.168.10.16:9200/store/books/1 -d {
  "title": "Elasticsearch: The Definitive Guide",
  "name" : {
    "first" : "Zachary",
    "last" : "Tong"
  },
  "publish_date":"2016-02-06",
  "price":"99.99"
}

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

curl -XPOST http://192.168.10.16:9200/store/books/1/_update -d {
  "doc": {
     "price" : 88.88
  }
}

4、DELETE操作

curl -XDELETE http://192.168.10.16:9200/store/books/1

5、查询所有(_search)

http://hadoop100:9200/store/books/_search

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!