一、Es Java API方法6.0.1查询总条数方法
TransportClient client = ElasticSearchUtil.getClient("rbc.es","192.168.30.230", 9300);
SearchResponse response = client.prepareSearch("liuwunan").setTypes("doc").get();
long searchHits = response.getHits().getTotalHits();
二、ElasticSearch查询基本查询数据方法
三、ElasticSearch 使用 Postman查询是否存在某字段数据条数方法
POST : http://192.168.1.201:9200/liuwunan/doc/_search
{
"query": {
"bool": {
"must": {
"exists": {
"field": "字段名"
}
}
}
}
}
四、ElasticSearch 查询修改的方法
将id等于1001的c_name改成"张三
POST http://192.168.1.100:9200/liuwunan/doc/1001/_update?pretty
{
"doc": {
"c_name": "张三"
}
}
五、ElasticSearch 删除数据的方法
POST http://172.29.9.29:9200/liuwunan/doc/_delete_by_query?pretty
{
"query": {
"term": {
"name_type": "SE"
}
}
}
六、Postman查询方法一页查询多条数据的方法
POST http://192.168.1.201:9200/索引名/索引类型/_search
from ,size(分页)
{
"from": 0,
"size": 100
}
七、ElasticSearch查询语法
POST 192.168.1.100:9200/liuwunan/_doc/_search
八、精准查询:term(精确查找)
{
"query": {
"term": {
"name": "交通台数据"
}
}
}
如果创建的Mapping的字段进行了分组处理
{
"name": {
"type": "text",
"index": "true",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
在查询的时候需要使用字段名.raw
{"query":{"term":{"name.raw":"交通台数据"}}}
指定查询的数据字段为关键字,如果不分词查询,因为不指定raw查询的时候,会针对数据进行分词,当输入想要的数据进行查询的时候,你会无法精准查询到! 加上了raw,会对数据设定为keyword匹配,不分词,即可达到查询的效果!
九、match(分词模糊匹配)
POST 192.168.1.100:9200/liuwunan/_doc/_search
{
"query": {
"match": {
"name": "交通台"
}
}
}
相当于SQL的like方法:Select * from test where name like '%交通台%'
十、must (并且查询)
POST 192.168.1.100:9200/liuwunan/_doc/_search
查询名称“张三”,并且city_code为“10000”的数据
{
"query": {
"bool": {
"must": [{
"term": {
"name": "张三"
}
},
{
"term": {
"city_code": "10000"
}
}
]
}
}
}
十一、should (或者查询)
POST 192.168.1.100:9200/liuwunan/_doc/_search
查询city_name“北京市”或者country_name为“中国”的数据
{
"query": {
"bool": {
"should": [{
"term": {
"city_name": "北京市"
}
},
{
"term": {
"country_name": "中国"
}
}
]
}
}
}
十二、_source(返回字段)
查询类型为SE的数据并返回id和name两个字段,多个字段可以继续追加数组中去
POST 192.168.1.100:9200/liuwunan/_doc/_search
{
"_source": {
"include": ["id", "name"]
},
"query": {
"match": {
"name": "交通台"
}
}
}
十三、ElasticSearch 修改当前索引的在结果窗口展示条数,默认限制在10000条
PUT:http://IP:9200/索引名/_settings/
PUT: http://47.94.16.230:9200/索引名/_settings/
{
"index": {
"max_result_window": 100000000
}
}
十四、查询出当前ES的所有索引
http://192.168.1.201:9200/_cat/indices
十五、ElasticSerach删除索引所有数据,可以按类型删除,先按类型查询,后删除
POST indexName/index_type/_delete_by_query
http://192.168.1.200:9200/liuwunan/doc/_delete_by_query
{
"query": {
"match_all": {}
}
}
查询出所有并且删除所有数据
十六、时间区间查询
{
"query": {
"range" : {
"create_time": {
"gte": "2019-02-01",
"lte": "2019-02-09"
}
}
}
}