elasticsearch基础语法

1)新增商品:新增文档,建立索引
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/13)修改商品:替换文档
PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

(4)修改商品:更新文档
POST /ecommerce/product/1/_update
{
  "doc": {
    "name": "jiaqiangban gaolujie yagao"
  }
}

(5)删除商品:删除文档
DELETE /ecommerce/product/1
------------------------------------------------
GET /ecommerce/product/_search
{
  "query": {
    "match_all": {

    }
  },
  "_source": ["name","price"], //显示指定列
  "from": 1,
  "size": 1, 
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

-------------------------------------------------------

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

    }
  }
}
-------------------------------------------------------

----  插入一条
PUT /ecommerce/product/4
{
  "name":"special yagao",
  "desc":"special meibai",
  "price":50,
  "producer":"special yagao producer",
  "tags":["meibai"]
}

-------------------------------------------------------

----  按producer分词查询 
GET /ecommerce/product/_search
{
  "query":{
    "match":{
       "producer":"yagao producer"
    }
  }
}

-------------------------------------------------------

----  短语搜索
GET /ecommerce/product/_search
{
  "query":{
    "match_phrase":{
       "producer":"yagao producer"
    }
  }
}

-------------------------------------------------------

----  高亮
GET /ecommerce/product/_search
{
  "query":{
    "match":{
       "producer":"yagao producer"
    }
  },
  "highlight": {
    "fields": {
      "producer":{}
    }
  }
}

-------------------------------------------------------
第一个 计算每一个tags下的商品数量
GET /ecommerce/product/_search
{
  "size": 0, //去除他所检索到的数据
  "aggs": { //aggs聚合
    "group_by_tags": { //聚合名字
      "terms": { //terms是按照指定field进行分组计算
        "field": "tags"
      }
    }
  }
}

上面报错执行
PUT ecommerce/_mapping/product/
{
  "properties": {
    "tags": { //指定列设为fielddata=true
      "type":"text",
      "fielddata": true
    }
  }
}

-------------------------------------------------------

包含yagao的商品,计算每个商品下的tags
GET /ecommerce/product/_search
{
  "size": 0, 
  "query": {
    "match": {
      "name": "yagao"
    }
  }, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }
}

-------------------------------------------------------
先分组 再算均值 计算每个tag下商品均价
GET /ecommerce/product/_search
{
  "size": 0, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      },
      "aggs": {
        "avg_price": {
        "avg": {
          "field": "price"
        }
      }
      }
    }
  }
}

上面要求降序排序
GET /ecommerce/product/_search
{
  "size": 0, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags",
        "order":{"avg_price":"desc"} //新增此段
      },
      "aggs": {
        "avg_price": {
        "avg": {
          "field": "price"
        }
      }
      }
    }
  }
}

-------------------------------------------------------

价格范围区间分组,然后在每组内再按照tag分组,最后计算每组平均价格
GET /ecommerce/product/_search
{
  "size": 0,
   "aggs": {
     "group_by_price": {
       "range": {
         "field": "price",
         "ranges": [
           {
             "from": 0,
             "to": 20
           },
           {
             "from": 20,
             "to": 40
           },
           {
             "from": 40,
             "to": 60
           }
         ]
       },
       "aggs": {
          "group_by_tags": {
           "terms": {
             "field":"tags"
           },
           "aggs": {
               "avg_price": {
                 "avg": {
                   "field": "price"
                 }
               }
             }
         }
       }
     }
   }
}

es语法在Java中的应用:
https://elasticsearch.cn/article/102















你可能感兴趣的:(elasticsearch基础语法)