Elastic_Dev_Tools


GET _search
{
  "query": {
    "match_all": {}
  }
}

GET /_analyze
{
  "analyzer": "ik_smart",
  "text": "我爱北京天安门"
}
# 创建表
PUT /user
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer",
        "index": true
      },
      "info": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email": {
        "type": "keyword"
      },
      "name": {
        "type": "object", 
        "properties": {
          "firstName": {
            "type": "keyword"
          },
          "lastName": {
            "type": "keyword"
          }
          
        }
      }
    }
  }
}
#查看索引库
GET /hotel
#删除索引库
DELETE /user
#修改索引库
PUT /user/_mapping
{
  "properties": {
    "weight": {
      "type": "double"
    }
  }
}

#文档操作
#新建文档
POST /user/_doc/
{
  "age":30,
  "email":"张@qq.com",
  "info":"JAVA高级程序员",
  "name":{
    "firstName":"张",
    "lastName": "123"
  },
  "weight":"75"
}
#查询索引库所有数据
GET /user/_search
{
  "query": {
    "match_all": {}
  }
}
#查询某一个文档数据
GET /user/_doc/1



#删除某一个文档数据
DELETE /user/_doc/lDD-FJMBA6ani3V3HjCM


#修改文档数据


#全局修改
PUT /user/_doc/1
{
  "age":30,
  "email":"jianghuanhuan@qq.com",
  "info":"JAVA高级程序员",
  "name":{
    "firstName":"蒋",
    "lastName":"欢欢"
  },
  "weight":"75"
}


#局部修改
POST /user/_update/1
{
  "doc": {
    "age":66
  }
}

#创建hotel索引库
PUT /hotel
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type": "integer"
      },
      "score":{
        "type": "integer"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "city":{
        "type": "keyword",
        "copy_to": "all"
      },
      "starName":{
        "type": "keyword",
         "copy_to": "all"
      },
      "business":{
        "type": "keyword"
      },
      "location":{
        "type": "geo_point"
      },
      "pic":{
        "type": "keyword",
        "index": false
      },
      "isAd":{
        "type": "text"
      },
      "all":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
GET /hotel
DELETE /hotel
#查询索引库所有数据
GET /hotel/_search
{
  "query": {
    "match_all": {}
  }
}
#全文检索的match查询
GET /hotel/_search
{
  "query": {
    "match": {
      "all": "如家"
    }
  }
}
#全文检索的multi_match查询
GET /hotel/_search
{
  "query": {
    "multi_match": {
      "query": "如家",
      "fields": ["brand","city","name"]
    }
  }
}
#精确查询term
GET /hotel/_search
{
  "query": {
    "term": {
      "brand": {
        "value": "丽笙"
      }
    }
  }
}
#精确查询range
GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 200,
        "lte": 300
      }
    }
  }
}

#地理坐标查询 geo_distance
GET /hotel/_search
{
  "query": {
    "geo_distance":{
      "distance": "1.5km",
      "location": "31.21,121.5"
    }
  }
}

#复合型查询:function_score---广告置顶
GET /hotel/_search
{
  "query": {
  "function_score": {
    "query": {
      "match": {
        "all": "如家"
      }
    },
    "functions": [
      {
        "filter": {
         "range": {
           "price": {
             "gte": 300,
             "lte": 450
           }
         }
        },
        "weight": 10
      } 
    ],
    "boost_mode": "multiply"
  }   
  }
}


GET /hotel/_search
{
"sort": [
  {
    "price": {
      "order": "desc"
    }
  },
  
  {
    "_geo_distance": {
      "location": "31.25,121.5",
      "order": "asc"
    
     }
   }
  ] 
}
  


GET /hotel/_search---广告置顶
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "all": "如家"
        }
      },
      "functions": [
        {
         "filter": {
           "term": {
             "city": "北京"
           }
         },
         "weight": 10
        }
       
      ]
    }
  }
}

GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "geo_distance": {
            "distance": "10km",
            "location": {
              "lat": 31.21,
              "lon": 121.5
            }
          }
        }
      ]
    }
  }
}



GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "term": {
            "city": "北京"
          }
        },
        {
          "term": {
            "brand": "如家"
          }
        }
      ]
    }
  },
  "sort": [
    {
     "_geo_distance": {
       "location": {
         "lat": 31.25,
         "lon": 121.5
       },
       "order": "asc"
     }
    },
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "highlight": {
    "fields": {
      "name": {
        "pre_tags": "<em>",
        "post_tags": "</em>",
        "require_field_match": "false"
      }
    }
  }
}


#结果集排序
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 31.21,
          "lon": 121.5
        },
        "order": "asc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "score": {
        "order": "desc"
      },
      "price": {
        "order": "asc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "isAD": {
        "order": "desc"
      }
    }
  ]
}


# 分页
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "from": 191,
  "size": 10,
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ]
}


# 高亮
GET /hotel/_search
{
  "query": {
    "match": {
      "name": "如家"
    }
  },
  "highlight": {
    "fields": {
      "name": {
       "pre_tags": "<em>",
       "post_tags": "</em>"
      }
    }
  }
}

GET /hotel/_search
{
  "query": {
    "match": {
      "isAD": true
    }
  },
  "highlight": {
    "fields": {
      "name": {
        "pre_tags": "<em>",
        "post_tags": "</em>",
        "require_field_match": "false"
      }
    }
  }
}

POST /hotel/_update/197837109
{
    "doc": {
        "isAD": true
    }
}
POST /hotel/_update/1557882030
{
    "doc": {
        "isAD": true
    }
}
POST /hotel/_update/197492277
{
    "doc": {
        "isAD": true
    }
}
POST /hotel/_update/200214824
{
    "doc": {
        "isAD": true
    }
}





GET /hotel/_search
{
  "match": {
   "isAD": true
  }
}

GET /hotel/_search
{
  "query": {
    "match": {
      "all": "上海"
    }
  }
}





#对品牌(brand)分组  做聚合, 查出价格在500-1000的


#size: 0 的作用是告诉 Elasticsearch不要返回任何匹配的文档,只返回聚合结果。这样可以减少网络传输的数据量,提高#查询效率,尤其是在处理大量数据时#doc_count 值表示每个分组(bucket)中包含的文档数量。
#无论你使用哪种聚合类型(如 terms、date_histogram、range 等),每个分组都会有一个 doc_count 值,表示该分组中包含的文档数量。






GET /hotel/_search
{
  "query": {
   "range": {
     "price": {
       "gte": 500,
       "lte": 1000
     }
   }
  },
  "size": 0, 
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 5,
        "order": {
          "_count": "desc"
        }
      }
    }
  }
}




#求某个数据的最大值,最小值
#stats求所有情况 


GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "size": 0, 
  "aggs": {
    "priceAgg": {
      "stats": {
        "field": "price"
      }
    }
  }
}

#度量聚合= 桶聚合+管道聚合
GET /hotel/_search
{
"query": {
   "range": {
     "price": {
       "gte": 500,
       "lte": 1000
     }
   }
  },
  "size": 0, 
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 10,
        "order": {
          "_count": "desc"
        }
      },
      "aggs": {
        "priceAgg": {
          "stats": {
            "field": "price"
          }
        }
      }
    }
  }
}



GET /hotel/_search
{
"query": {
  
   "match_all": {
    
   }
  },
  "size": 0, 
  "aggs": {
     "cityAgg": {
      "terms": {
        "field": "city",
        "size": 10,
        "order": {
          "_key": "asc"
        }
      }
    }
  }
}


GET /hotel/_search
{
"query": {
  
   "match_all": {
    
   }
  },
  "size": 0, 
  "aggs": {
    "brandAgg": {
      "terms": {
        "field": "brand",
        "size": 10,
        "order": {
          "_key": "asc"
        }
      }
    },
     "priceAgg": {
      "terms": {
        "field": "price",
        "size": 10,
        "order": {
          "_key": "asc"
        }
      }
    },
     "starNameAgg": {
      "terms": {
        "field": "starName",
        "size": 10,
        "order": {
          "_key": "asc"
        }
      }
    },
     "cityAgg": {
      "terms": {
        "field": "city",
        "size": 10,
        "order": {
          "_key": "asc"
        }
      }
    }
  }
}



GET /hotel/_search
{
  "query": {
    "match": {
      "id": "38609"
    }
  }
  
}


GET /hotel/_search
{
  "query": {
    "match": {
      "name": "2222222"
    }
  }
}





# 创建商城的索引结构(表结构)
PUT /item
{
  "mappings": {
    "properties": {
      "id":{
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "price":{
        "type": "long"
      },
      "stock":{
        "type": "integer"
      },
      "image":{
        "type": "keyword",
        "index": false
      },
      "category":{
        "type": "keyword",
        "copy_to": "all"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "spec":{
        "type": "keyword"
      },
      "sold":{
        "type": "integer"
      },
      "commentCount":{
        "type": "integer"
      },
      "status":{
        "type": "integer"
      },
      "isAD":{
        "type": "boolean"
      },
      "all":{
        "type": "text",
          "analyzer": "ik_max_word"
      }
        
      }
    }
  }
  



#查看索引结构
GET /item
DELETE /item

#增加文档

POST /mapping/_doc
{

  "id": "12345",
  "name": "Apple iPhone 12",
  "price": 799,
  "stock": 100,
  "image": "https://example.com/iphone12.jpg",
  "category": "Smartphones",
  "brand": "Apple",
  "spec": "6.1-inch display, 5G",
  "sold": 50,
  "commentCount": 20,
  "status": 1,
  "isAD": true
}

#查询所有
GET /item/_search
{
  "query": {"match_all": {}}
}


DELETE /item

#插入多条数据
POST /mapping/_doc 
{
  
}


DELETE /item

GET /item/_search
{
  "query": {
    "match_all": {
      
    }
  }
}

#查询指定文档数据
GET /item/_search
{
  "query": {
    "match": {
      "id": "584387"
    }
  }
}



#查询所有条数
GET /item/_count
{
  "query": {
    
    "match_all": {
      
    }
    
  }
}



posted @ 2024-11-14 08:40  加油酱  阅读(3)  评论(0编辑  收藏  举报