//查询所有
GET _search
{
"query": {
"match_all": {}
}
}
//创建一个索引(类似数据库)
PUT /heima
{
"settings": {
"number_of_shards": 1,//有多少分片,分布式,有几个机器就设置几
"number_of_replicas": 0//备份
}
}
GET /heima
DELETE /heima
//创建映射(类似数据表)
PUT /heima/_mapping/goods
{
"properties": {
"title":{
"type": "text",
"analyzer": "ik_max_word"//使用分词插件进行分词
},
"images":{
"type": "keyword",
"index": false
},
"price":{
"type": "float"
}
}
}
POST /heima/goods
{
"title":"小米手机",
"images":"http://www.baidu.com",
"price":1699.00
}
POST /heima/goods/1
{
"title":"小米手机",
"images":"http://www.baidu.com",
"price":1699.00
}
//查询所有
GET /heima/_search
{
"query": {
"match_all": {}
}
}
//删除id为lbjzK3EB7kI7c5hUnFzf的数据
DELETE /heima/goods/lbjzK3EB7kI7c5hUnFzf
POST /heima/goods/2
{
"title":"小米电视",
"images":"http://www.baidu.com",
"price":3699.00
}
POST /heima/goods/3
{
"title":"华为手机",
"images":"http://www.baidu.com",
"price":1699.00,
"subTitle":"小米"
}
//多字段搜索
GET /heima/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields": ["title","subTitle"]
}
}
}
//词条搜索
GET /heima/_search
{
"query": {
"term": {
"title": {
"value": "小米"
}
}
}
}
//多词条搜索
GET /heima/_search
{
"query": {
"terms": {
"title": [
"小米",
"华为"
]
}
}
}
GET /heima/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "小米"
}
},
{
"term": {
"price": {
"value": "1699"
}
}
}
]
}
}
}
GET /heima/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
},
{
"term": {
"price": {
"value": "1699"
}
}
}
]
}
}
}
GET /heima/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "小米"
}
},
{
"terms": {
"price": [
1699,1899
]
}
}
]
}
}
}
GET /heima/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 20000
}
}
}
}
GET /heima/_search
{
"query": {
"range": {
"price": {
"gte": 10,
"lte": 20000
}
}
}
}
GET /heima/_search
{
"query": {
"fuzzy": {
"title":{
"value": "大机",
"fuzziness": 2
}
}
}
}
GET /heima/_search
{
"size": 0,
"aggs": {
"brandAggs": {
"terms": {
"field": "brand"
},
"aggs": {
"price_avg": {
"avg": {
"field": "price"
}
}
}
}
}
}
网友评论