elasticsearch crud和dsl

以下操作在es 7.x版本下执行

CRUD

批量添加数据
POST /company/_bulk
添加单条数据,一般用POST
POST /company/_doc/1
{
    
    "account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"[email protected]","city":"Brogan","state":"IL"}
更新数据,一般用PUT
PUT /company/_doc/1
{
    
    "account_number":1,"balance":2222,"firstname":"Amber2","lastname":"Duke2","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"[email protected]","city":"Brogan","state":"IL"}
查找所有数据
GET /company/_search
{
    
      
	"query": {
    
    
		"match_all": {
    
    } 
	} 
}
查找指定id数据
GET /company/_doc/1
删除指定id数据
DELETE /company/_doc/1

检索

分页查找

from:开始页
size:每页显示记录数

GET /company/_search 
{
    
    
  "query": {
    
    
    "match_all": {
    
    }
  },
  "from": 0,
  "size": 20
}
显示指定字段

_source

GET /company/_search 
{
    
    
  "query": {
    
    
    "match_all": {
    
    }
  },
  "_source": [
    "account_number", "balance"
  ],
  "from": 0,
  "size": 5
}
按指定字段查询

match 模糊搜索,进行自动分词

GET /company/_search 
{
    
    
  "query": {
    
    
    "match": {
    
    
      "address": "Road"
    }
  }
}

match和match_phrase 精确查询 ,不进行分词

GET /company/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "address.keyword": "263 Aviation Road"
    }
  }
}{
    
    
  "query": {
    
    
    "match_phrase": {
    
    
      "address.keyword": "263 Aviation Road"
    }
  }
}
多字段查询

multi_match

GET /company/_search
{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "Albemarle AK",
      "fields": ["address","state"]
    }
  }
}
复合查询

bool 复合查询
must:必须
must_not:必须不能
should:应该,should只影响相关评分
例子:查询address里面包含305,state不能包含KS,如果年龄>=20并且<=34就最好

GET /company/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
            "address": "305"
          }
        }
      ], 
      "must_not": [
        {
    
    
          "match": {
    
    
            "state": "KS"
          }
        }
      ],
      "should": [
        {
    
    "range": {
    
    
          "age": {
    
    
            "gte": 20,
            "lte": 34
          }
        }}
      ]
    }
  }
}
过滤 filter

filter可以替换 must, filter不需要评分,效率比query好

GET /company/_search 
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "filter": {
    
    
        "match": {
    
    
          "gender": "M"
        }
      }
    }
  }
}
term精确查询

term/terms只能用于非文本类型(string)
一、term 匹配单个值
二、terms 匹配多个值

{
    
    
  "query": {
    
    
    "term": {
    
    
      "age": {
    
    
        "value": "25"
      }
    }
  }
}
{
    
    
  "query": {
    
    
    "terms": {
    
    
      "age": [
        "22",
        "30"
      ]
    }
  }
}

以下查询文本类型,会查不到数据

{
    
    
  "query": {
    
    
    "term": {
    
    
      "gender": {
    
    
        "value": "M"
      }
    }
  }
}

每一个text类型的字段,都有一个keyword,用来精确匹配,以下内容可以查找到数据

GET /company/_search 
{
    
    
  "query": {
    
    
    "term": {
    
    
      "gender.keyword": {
    
    
        "value": "M"
      }
    }
  }
}

聚合查询:用来分析数据

查看平均薪水

aggs:聚合查询,
avg_balance:指定查询的名称,任意有效字符串
avg:关键字,用于查询平均值
field:用于指定查询哪个字段

GET /company/_search
{
    
    
  "aggs": {
    
    
    "avg_balance": {
    
    
      "avg": {
    
    
        "field": "balance"
      }
    }
  }
}
统计

统计地址中包含Street并按年龄进行分组

GET /company/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "address": "Street"
    }
  },
  "aggs": {
    
    
    "age_count": {
    
    
      "terms": {
    
    
        "field": "age",
        "size": 100
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/coffee_shop/article/details/104959341