1、Elasticsearch创建索引
创建索引
PUT /my_temp_index
{
"settings":{
"number_of_shards":3,
"number_of_replicas":2
}
}
number_of_shards:每个索引的主分片数,默认值是5。这个配置在索引创建后不能修改
number_of_replicas:每个主分片的副本数,默认值是1。对于活动的索引库,这个值可以随时修改。
修改settings值
PUT /my_temp_index/_settings
{
"number_of_replicas":1
}
2、创建mappings
每个文档都会有type,每种type都会有自己的mappings,且一个索引都只对应一个mappings,mappings定义了type(索引)的field(字段),这里列举了几种类型的字段创建方式
▶ 字符串 string
● text
● keyword
▶ 数字
● long
● integer
● short
● byte
● double
● float
● half_float
● scaled_float
▶ 布尔类型 boolean
● boolean
▶ 日期
● date
POST type/mappings/_mapping
{
"dynamic": "strict",
"properties": {
"id": {
"type": "integer"
},
"companyname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"analyzer": "ik_max_word"
},
"type": {
"type": "text"
},
"pubtime": {
"type": "date"
}
}
}
3、ES6常用查询操作
单一查询
POST type/mappings/_id
纠错查询
POST type/mappings/_search
{
"query":{
"match":{
"title":{
"query":"包银铁路:银至惠农段中标结局",
"fuzziness":2
}
}
}
}
精确匹配某个字段的值
POST /type/mapping/_search?
{
"query": {
"bool": {
"must": [
{
"term": {
"id":1
}
}
]
}
}
}
相似度查询
POST type/mappings/_search
{
"query": {
"match": {
"title": {
"query": "光雾山高速公路",
"minimum_should_match": "75%"
}
}
}
}
更新操作
POST /ccccltd/shipping/stang_bid_new_91961699/_update
{
"doc" : {
"visible": 1
}
}
批量更新
POST talent_market_recruit/recruit/_update_by_query
{
"query": {
"bool": {
"must": [
{
"range": {
"uid": {
"gt":0
}
}
}
]
}
},
"script": {
"source": "ctx._source['data_status']='0'"
}
}
查看字段信息
GET /go/adjdocument/_mapping
查询所有并进行排序
GET type/mapping/_search
{
"query" : {
"match_all": {}
},
"sort": [
{
"add_time": "desc"
}
]
}
添加一条数据
POST /go/adjdocument/test0005
{
"title": "这是一条测试数据",
"info": "这是一条测试数据这是一条测试数据这是一条测试数据",
"institutions": "",
"author": "阮持涛、华安财产保险股份有限公司北京分公司"
}
删除一条数据——慎用
DELETE /go/adjdocument/test0005
查询存在字段
GET /go/adjdocument/_search
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "title"
}
}
]
}
}
}
查询不存在字段
GET /go/adjdocument/_search
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "type"
}
}
]
}
}
}
查询字段值长度为“”,长度为0
GET /go/adjdocument/_search
{
"query": {
"script": {
"script": "doc['institutions'].length > 0"
}
}
}
wildcard通配符查询
GET /go/adjdocument/_search
{
"query": {
"regexp": {
"author": "北京"
}
}
}
根据pubtime简单排序并指定查询数量
POST /ccccltd/shipping/_search
{
"query": {
"term": {
"cate_id": {
"value": 0
}
}
},
"sort": [
{
"pub_time": {
"order": "desc"
}
}
],
"size": 6
}
简单bool逻辑查询
POST /ccccltd/shipping/_search?
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"trade_label": "公路工程"
}
},
{
"term": {
"cate_id": 2
}
},
{
"term": {
"area_id": 26
}
}
]
}
}
}
范围条件查询
POST /ccccltd/shipping/_search?
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"trade_label": "公路工程"
}
},
{
"term": {
"cate_id": 2
}
},
{
"term": {
"area_id": 26
}
},
{
"range": {
"pub_time": {
"gte": 1566518400000,
"lte": 1576518400000
}
}
}
]
}
}
}
复合bool逻辑
GET /win/gooddatas/_search?
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"group": "中国中铁"
}
},
{
"match_phrase": {
"trade_label": "市政工程"
}
},
{
"bool": {
"should": [
{
"term": {
"area": 24
}
},
{
"term": {
"area": 26
}
}
]
}
},
{
"range": {
"pub_time": {
"gte": "2019-12-17"
}
}
}
]
}
},
"sort": [
{
"pub_time": {
"order": "asc"
}
}
]
}
否定逻辑查询
GET /ccccltd/shipping/_search?size=10
{
"query": {
"bool": {
"must": [
{
"range": {
"pub_time": {
"gte": 1514736000000,
"lte": 1589510692000
}
}
},
{
"match_phrase": {
"cate_id": 2
}
},
{
"match_phrase": {
"author": "四川省公共资源交易信息网"
}
}
],
"should": [
{
"match_phrase_prefix": {
"trade_label": "公路工程"
}
},
{
"match_phrase_prefix": {
"trade_label": "市政工程"
}
}
],
"must_not": [
{
"bool": {
"should": [
{
"match_phrase": {
"trade_label": "物资设备采购"
}
},
{
"match_phrase": {
"title": "合同"
}
}
]
}
}
]
}
}
}
查询某个字段最大值
GET /ccccltd/shipping/_search?
{
"query": {
"term": {
"table_name": {
"value": "stang_bid_new"
}
}
},
"aggs" : {
"max_id" : {
"max" : {
"field" : "id"
}
}
},
"size":0
}
经纬度范围查询
GET plj/Architectures/_search
{
"query": {
"constant_score": {
"filter": {
"geo_bounding_box": {
"gps_location": {
"top_right": {
"lat":30.8810121377,
"lon":104.3344116211
},
"bottom_left": {
"lat":30.4463058675,
"lon":103.7796020508
}
}
}
}
}
}
, "size": 20
}
距离远近查询
GET plj/Architectures/_search
{
"query": {
"constant_score": {
"filter": {
"geo_distance": {
"distance": "10km",
"gps_location": {
"lat": 30.657258,
"lon": 104.065933
}
}
}
}
},
"sort": [
{
"_geo_distance": {
"gps_location": [
{
"lat": 30.657258,
"lon": 104.065933
}
],
"unit": "km",
"distance_type": "arc",
"order": "asc",
"validation_method": "STRICT"
}
}
]
}
简单聚合查询
POST /ccccltd/shipping/_search
{
"query": {
"term": {
"cate_id": {
"value": 2
}
}
},
"aggs": {
"pubtime_aggs": {
"terms": {
"field": "pub_time",
"size": 30
}
}
},
"size": 0
}
聚合嵌套
POST /ccccltd/shipping/_search
{
"query": {
"range": {
"pub_time": {
"gte": 159600865000
}
}
},
"aggs": {
"project_label_aggs": {
"terms": {
"field": "trade_label.keyword",
"size": 10
},
"aggs": {
"area_id_aggs": {
"terms": {
"field": "area_id",
"size": 20
}
}
}
}
},
"size": 0
}
聚合并排序
POST /win/gooddatas/_search?
{
"query": {
"range": {
"pub_time": {
"gte": 1577808000000
}
}
},
"aggs": {
"area_aggs": {
"terms": {
"field": "area",
"size": 40
},
"aggs": {
"rated": {
"top_hits": {
"sort": [
{
"first_bid_moneys": {
"order": "desc"
}
}
],
"size": 3
}
}
}
}
},
"size": 0
}
改变属性
PUT /ccccltd/shipping/_mapping
{
"properties": {
"trade_label": {
"type": "text",
"fielddata": true,
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}