首页 > 其他分享 >ES API基本操作

ES API基本操作

时间:2022-10-07 23:23:56浏览次数:56  
标签:http 9200 GET shopping 192.168 API 基本操作 ES 0.110

创建索引,user为索引名称      

PUT   http://192.168.0.110:9200/user

查询索引基本信息

GET   http://192.168.0.110:9200/user

查询所有索引

GET   http://192.168.0.110:9200/_cat/indices?

删除索引

DELETE   http://192.168.0.110:9200/shopping

索引创建文档数据

POST   http://192.168.0.110:9200/shopping/_doc

{
    "title": "小米手机",
    "category": "小米",
    "images": "http://www.xiaomi.com",
    "price": 9999.00
}

主键查询

GET   http://192.168.0.110:9200/shopping/_doc/tHP1FoMBv-1e6_zbJ75F

索引全量查询

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "match_all": {  
        }
    }
}

全量修改数据

POST   http://192.168.0.110:9200/shopping/_doc/tHP1FoMBv-1e6_zbJ75F

{
    "title": "华为手机",
    "category": "华为",
    "images": "http://www.华为.com",
    "price": 4999.00
}

局部修改数据

POST   http://192.168.0.110:9200/shopping/_update/tHP1FoMBv-1e6_zbJ75F

{
    "doc": {
        "title": "华为手机"
    }
}

删除主键

DELETE   http://192.168.0.110:9200/shopping/_doc/s3PrFoMBv-1e6_zbHb6Z

条件查询

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "match": {
           "category": "小米"
        }
    }
}

分页查询

from 从第几条开始查
size 返回几条数据

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "match": {
           "category": "小米"
        }
    },
    "from": 2,
    "size": 3
}

条件+分页+过滤+排序

desc 降序
asc 升序

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "match": {
           "category": "小米"
        }
    },
    "from": 2,
    "size": 7,
    "_source": ["title", "price"],
    "sort": {
        "price": {
            "order": "asc"
        }
    }
}

多条件查询-同时成立

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "category": "小米"
                    }
                },
                {
                    "match": {
                        "price": 1999.00
                    }
                }
            ]
        }
    }
}

多条件查询+或+范围查询

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "category": "小米"
                    }
                },
                {
                    "match": {
                        "category": "华为"
                    }
                }
            ],
            "filter": {
                "range": {
                    "price": {
                        "gt": 5000
                    }
                }
            }
        }
    }
}

完全匹配查询

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "match_phrase": {
           "category": "小米"
        }
    }
}

高亮显示查询

GET   http://192.168.0.110:9200/shopping/_search

{
    "query": {
        "match_phrase": {
           "category": "小米"
        }
    },
    "highlight": {
        "fields": {
            "category": {}
        }
    }
}

聚合查询

GET   http://192.168.0.110:9200/shopping/_search

//{
//    "aggs": {
//        "price_groups": {  //随便起个名字
//            "terms": {  //固定格式,统计
//                "field": "price"    // 统计关键词
//            }
//        }
//    },
//    "size": 0  // 不显示原始数据
//}

{
    "aggs": {
        "price_avg": {  //随便起个名字
            "avg": {  //固定格式,平均值
                "field": "price"    // 统计关键词
            }
        }
    },
    "size": 0  // 不显示原始数据
}

映射创建索引

PUT  http://192.168.0.110:9200/user/_mapping

{
    "properties": {
        "name": {
            "type": "text", //可以分词查询
            "index": true
        },
        "sex": {
            "type": "keyword", //不可以分词查询,完全匹配查询
            "index": true            
        },
        "tel": {
            "type": "keyword",
            "index": false     // 不能按索引查询                  
        }
    }
}

统计一天进了多少条数据

topicanalysismsgl/_count


{
  "query": {
    "range": {
      "createTime": {
        "from": "2022-10-07T01:00:00+08:00",
        "to": "2022-10-07T18:40:45+08:00"
      }
    }
  }
}

 

标签:http,9200,GET,shopping,192.168,API,基本操作,ES,0.110
From: https://www.cnblogs.com/rtnb/p/16767490.html

相关文章