侧边栏壁纸
博主头像
liuxy博主等级

细水长流,吃穿不愁

  • 累计撰写 28 篇文章
  • 累计创建 26 个标签
  • 累计收到 5 条评论

目 录CONTENT

文章目录
Es

elasticsearch中常用的简单查询语句

liuxy
2021-05-14 / 0 评论 / 2 点赞 / 323 阅读 / 1,055 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-08-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

开始创建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" ]
}
PUT /ecommerce/product/4
{
        "name" : "yagao",
        "desc" :    "zhiwu",
        "price" :    56,
        "producer" :"111111 producer",
        "tags": [ "qingxin" ]
}
PUT /ecommerce/product/5
{
        "name" : "霸王",
        "desc" :    "霸王药业",
        "price" :    156,
        "producer" :"霸王 霸王药业",
        "tags": [ "国产" ]
}

63C9C94B7B6F42839278354B60973B4F.png

#查询

GET /ecommerce/product/5

93E75FF5D97E4392B0923D42EB8DAB0E.png

#覆盖,替换文档(全量替换)

PUT /ecommerce/product/1
{
    "name" : "11111",
    "desc" : " meibai",
    "price" : 44,
    "producer" : "gaolujie producer",
    "tags" : [ "meibai" , "fangzhu" ]
}

#分页查询

GET /ecommerce/product/_search
{
    "query" : { "match_all" : {}},
    "from" : 1,
    "size" : 10
}

CB27FDEB8CFF4BE9BC4ABABA519F96B7.png
![673F03DB47E34D5BAC4C4DF8A8BC7260.png]

#指定查询项

GET /ecommerce/product/_search
{
    "query" : {"match_all" : {}},
    "_source" : ["name", "price"]
}

ABC004F3F86A4C4AA53B85960AA2E9BA.png

#过滤查询

#搜索商品名称包含yaogao,而且售价大于25元的商品

GET /ecommerce/product/_search
{
        "query" : {
            "bool" : {
                "must" : {
                    "match" : {
                        "name" : "yagao"
              }
        },
        "filter" : {
            "range" : {
                "price": {"gt" : 50}
                    }
                }
            }
        }
    }

C21662E2ED924AA1A86110ED8384895B.png

#ull-text search(全文检索)

GET /ecommerce/product/_search
    {
        "query" : {
            "match" : {
                "producer": "producer"
            }
        }
    }
    

#phrase search(短语搜索)

GET /ecommerce/product/_search
    {
        "query" : {
            "match_phrase" : {
                "producer" : "producer"
            }
        }
}

EB4D4C1C6A75486E99490C0888381D1D.png

#多条件查询

#名字中有"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
          }
      }
}

D1AC052A410A4752849A0EDA7853CAF6.png

#Join全量检索

GET /ecommerce/_search
{
    "query": {
        "match_all": {}
    },
    "sort": ["_id"]
}

DC71E4023C4C443B9D4E565254059487.png
#测试

GET /ecommerce/_search
{
    "query": {
        "term": {
            "name": "11111"
        }
    }
}

---------------------------------------------华丽的分割线--------------------------------------------------

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" ]
}
PUT /ecommerce/product/4
{
        "name" : "yagao",
        "desc" :    "zhiwu",
        "price" :    56,
        "producer" :"111111 producer",
        "tags": [ "qingxin" ]
}
PUT /ecommerce/product/5
{
        "name" : "霸王",
        "desc" :    "霸王药业",
        "price" :    156,
        "producer" :"霸王 霸王药业",
        "tags": [ "国产" ]
}

#覆盖,替换文档(全量替换)
PUT /ecommerce/product/1
{
    "name" : "11111",
    "desc" : " meibai",
    "price" : 44,
    "producer" : "gaolujie producer",
    "tags" : [ "meibai" , "fangzhu" ]
}

#查询
GET /ecommerce/product/1

#分页查询
GET /ecommerce/product/_search
{
    "query" : { "match_all" : {}},
    "from" : 0,
    "size" : 10
}

#指定查询项
GET /ecommerce/product/_search
{
    "query" : {"match_all" : {}},
    "_source" : ["name", "price"]
}

#过滤查询
#搜索商品名称包含yaogao,而且售价大于50元的商品
GET /ecommerce/product/_search
{
        "query" : {
            "bool" : {
                "must" : {
                    "match" : {
                        "name" : "yagao"
              }
        },
        "filter" : {
            "range" : {
                "price": {"gt" : 50}
                    }
                }
            }
        }
    }
    
#ull-text search(全文检索)
GET /ecommerce/product/_search
    {
        "query" : {
            "match" : {
                "producer": "producer"
            }
        }
    }
    
#phrase search(短语搜索)
GET /ecommerce/product/_search
    {
        "query" : {
            "match_phrase" : {
                "producer" : "producer"
            }
        }
}
#多条件查询
#名字中有"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
          }
      }
}
#Join全量检索
GET /ecommerce/_search
{
    "query": {
        "match_all": {}
    },
    "sort": ["_id"]
}

#测试
GET /ecommerce/_search
{
    "query": {
        "term": {
            "name": "11111"
        }
    }
}

2
广告 广告

评论区