搭建完kibana后,依次输入如下命令,熟悉下elasticsearch命令。
一、开始创建Document:
PUT /ecommerce/product/1
{
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
PUT /ecommerce/product/2
{
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags": [ "fangzhu" ]
}
PUT /ecommerce/product/3
{
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags": [ "qingxin" ]
}
二、增删改
#2.查询
GET /ecommerce/product/2
#3.覆盖,替换文档(全量替换)
PUT /ecommerce/product/1
{
"name" : "jiaqiangban gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags" : [ "meibai" , "fangzhu" ]
}
注意:1.document是不可变的,如果要修改document的内容,可以通过全量替换,直接对document 重新建立索引,替换里面所有的内容。 2.es会将老的document标记为deleted(逻辑删除),然后新增我们给定的一个document,当我们 创建越来越多的document的时候,es会在适当的时机在后台自动删除(物理删除)标记为deleted 的document。 3.替换必须带上所有的field,否则其他数据会丢失。 14
#4.修改(更新文档)
POST /ecommerce/product/1/_update
{
"doc" : {
"name" : "jiaqianban gaolujie yagao1"
}
}
#5.删除文档
DELETE /ecommerce/product/2
注意:在删除一个document之后,我们可以从侧面证明,它不是立即物理删除的,因为它的一些版本号信息还是保留的
#6.使用query DSL 请求查询
GET /ecommerce/product/_search
{
"query" : {
"match_all" : {}
}
}
三、查询搜索
#6.2分页查询
#总共3条商品,假设每页就显示1条商品,现在显示第2页,所以就查出来第2个商品
GET /ecommerce/product/_search
{
"query" : { "match_all" : {}},
"from" : 1,
"size" : 1
}
#6.3指定查询项
GET /ecommerce/product/_search
{
"query" : {"match_all" : {}},
"_source" : ["name", "price"]
}
#6.4过滤查询
#搜索商品名称包含yaogao,而且售价大于25元的商品
GET /ecommerce/product/_search
{
"query" : {
"bool" : {
"must" : {
"match" : {
"name" : "yagao"
}
},
"filter" : {
"range" : {
"price": {"gt" : 25}
}
}
}
}
}
#7.ull-text search(全文检索)
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"producer": "yagao producer"
}
}
}
#8.phrase search(短语搜索)
GET /ecommerce/product/_search
{
"query" : {
"match_phrase" : {
"producer" : "yagao producer"
}
}
}
#9.多条件查询
#名字中有"yagao",描述上可以有fangzhu也可以没有,价格不能是25元
GET /ecommerce/_search
{
"query" : {
"bool" : {
"must" : [
{
"match" : {
"name" : "yagao"
}
}
],
"should" : [
{
"match" : {
"desc" : "fangzhu"
}
},
{
"match" : {
"desc" : "caoben"
}
}
],
"must_not" : [
{
"match" : {
"price" :25
}
}
],
"minimum_should_match" : 1
}
}
}