您当前的位置: 首页 >> 八卦 > >> 内容页

es之增删改查-环球时快讯

2023-05-24 16:43:24 来源:博客园

查询

index:GET task_results/_search/

普通查询:{"query": {"bool": {"must": [{"match": {"task_id": "1363bb4a03e0ee456345edc70b381f04e01064a42773576e3fe8810b0fd9df42"}}]}}}

json嵌套查询:{"query": {"nested":{"path":"cloud_result","query":{"bool":{"must":[{"match":{"cloud_result.label":113718644}}]}}}}}


(资料图)

range 查询:{"from":0,"size":10,"query": {"nested":{"path":"cloud_result","query":{"bool":{"must":[{"match":{"cloud_result.label":113718644}},{"range":{"cloud_result.confidence":{"gte":0.8}}}]}}}}}

同时查询多个值,类型mysql中的in:{"query": {"bool": {"must": [{"match": {"project_id": "1"}},{"terms":{"incident_type":[1,2]}}]}}}

排序:{"from":0,"size":10,"query": {"nested":{"path":"cloud_result","query":{"bool":{"must":[{"match":{"cloud_result.label":113718644}},{"range":{"cloud_result.confidence":{"gte":0.8}}}]}}}},"sort":{"create_at":{"order":"desc"}}}

json嵌套+普通字段查询:{"query":{"bool" : {"must" : [{"nested" : {"path" : "cloud_result","query" : {"term" : {"cloud_result.label" :113718644 }} }},{"term" : {"project_id" : 29 } }]}}}

match和term查询的区别match

  • match的查询词会被分词
  • match_phrase 不会分词
  • match_phrase 可对多个字段进行匹配

term

  • term代表完全匹配,不进行分词器分析
  • term 查询的字段需要在mapping的时候定义好,否则可能词被分词。传入指定的字符串,查不到数据

bool联合查询

  • must should must_not
  • must 完全匹配
  • should 至少满足一个
  • must_not不匹配

es聚合查询:https://blog.csdn.net/qq_43588909/article/details/126381390

根据project_id分组:{ "size": 0,"aggs": {"aggs.demo01": {"terms": {"field": "project_id","size": 10,"order": {"_count": "asc" // 根据统计条数升序,desc降序}}}}}

聚合后having:{"size": 0,"aggs": {"groupName": {"terms": {"field": "source"},"aggs": {"countSource": {"terms": {"field": "source", "order": {"_count": "asc"}}},"having": {"bucket_selector": {"buckets_path": {"orderCount": "_count"},"script": {"source": "params.orderCount >1 "}}}}}}}

基于查询结果聚合: 查询project_id=12 and cloud_result.label=694547 group source{ "size": 0,"query":{"bool": {"must":[{"nested":{"path": "cloud_result","query":{"match":{"cloud_result.label" :694547}}}},

]}

},"aggs": {"groupName": {"terms": {"field": "source"},"aggs": {"countSource": {"terms": {"field": "source", "order": {"_count": "asc"}}},"having": {"bucket_selector": {"buckets_path": {"orderCount": "_count"},"script": {"source": "params.orderCount >1 "}}}}}}}

聚合后查询:{"query": {"range": {"Price": {"gte": 50,"lte": 500}}},"aggs": {"tags_bucket":{"terms": {"field": "Tags.keyword"}}},"post_filter": {"term": {"Tags.keyword": "营养"}}}

更新

1.覆盖式更新(会把旧数据替换掉)PUT student-001/_doc/1?refresh=true{"id": 1,"name": "student-1","year": 1}

2.部分更新把 id 为1的数据的 name 字段更新POST student-001/_update/1?refresh=true{"doc": {"name": "student-123"}}

如果数据不存在,则作为新数据添加;如果数据已经存在,则更新旧数据,那么我们可以添加参数 doc_as_upsertPOST student-001/_update/1?refresh=true{  "doc": {    "name": "student-23"  },  "doc_as_upsert": true}使用脚本的方式来更新数据POST student-001/_update/1?refresh=true{  "script": {    "source": "ctx._source.name="student-1""  }}

新增

批量添加数据:PUT _bulk{"index":{"_index":"student-001","_id":1}}{"id": 1,"name": "student-1","year": 1}{"index":{"_index":"student-001","_id":2}}{"id": 2,"name": "student-2","year": 2}{"index":{"_index":"student-001","_id":3}}{"id": 3,"name": "student-3","year": 3}{"index":{"_index":"student-001","_id":4}}{"id": 4,"name": "student-4","year": 1}{"index":{"_index":"student-001","_id":5}}

删除

根据查询条件删除:POST task_results/_delete_by_query{"query": {"bool": {"must": [{"match": {"project_id": 12}},{"match": {"organization_id": "maint"}}]}}}

关键词:
分享到:
x 广告
x 广告

  Copyright @ 2001-2013 www.9774.com.cn All Rights Reserved 中国时尚网 版权所有

联系方式:954 29 18 82 @qq.com

   粤ICP备18025786号  营业执照公示信息   未经吉中国时尚网书面授权,请勿建立镜像,转载请注明来源,违者依法必究

关于我们 | 联系方式 | 版权声明 | 招聘信息 | 友情链接 | 合作伙伴 |