Fork me on GitHub

ES调用方法

一、查询

  1. 根据某个字段分组后,再依据另一个字段排序后,取组内第一个数据
index/_search
{
        "from": 0,
        "size": 0,
        "aggs": {
          "youtube_video_th":{
            "terms":{
              "field": "video_id"
            },
            "aggs":{
              "my_top_hits":{
                "top_hits":{
                  "sort": [
              {
                "gather_time": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": [
                "video_id",
                "video_description",
                "video_name",
                "gather_time"
              ]
            },
                  "size":1
                }
              }
            }
          }
        }
}
  1. 查看某个索引下的数据结构

    /index/_mapping?pretty
    
  2. 实现select * from table where a and (b or c or d)

    {
        "bool": {
            "adjust_pure_negative": true,
            "boost": 1.0,
            "must": [
                {
                    "bool": {
                        "adjust_pure_negative": true,
                        "boost": 1.0,
                        "should": [
                            {
                                "match_phrase": {
                                    "content": {
                                        "boost": 1.0,
                                        "query": "管理员",
                                        "slop": 0,
                                        "zero_terms_query": "NONE"
                                    }
                                }
                            },
                            {
                                "match_phrase": {
                                    "content": {
                                        "boost": 1.0,
                                        "query": "A01",
                                        "slop": 0,
                                        "zero_terms_query": "NONE"
                                    }
                                }
                            },
                            {
                                "match_phrase": {
                                    "content": {
                                        "boost": 1.0,
                                        "query": "总经理",
                                        "slop": 0,
                                        "zero_terms_query": "NONE"
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "match_phrase": {
                        "source_keyword_type": {
                            "boost": 1.0,
                            "query": "sentiment",
                            "slop": 0,
                            "zero_terms_query": "NONE"
                        }
                    }
                }
            ]
        }
    }
    
  3. 查询后排序

    {
        "aggs": {},
        "from": 0,
        "query": {
            "bool": {
                "must": [],
                "must_not": [
                    {
                        "term": {
                            "site_name": ""
                        }
                 },
                    {
                        "term": {
                            "title": ""
                        }
                    }
                ],
                "should": [
                    {
                        "match_all": {}
                    }
                ]
            }
        },
        "size": 10,
        "sort": {
            "publish_time": {
                "order": "desc"
            }
        }
    }
    
  4. 简单的分组统计

    {
        "aggs": {
            "group_by_tags": {
                "terms": {
                    "field": "site_name",
                    "size": 50
                }
            }
        },
        "size": 0
    }
    

二、删除

/index/type/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

三、总体查询

  1. 查看所有已存在index

    /_cat/indices?v
    

四、创建索引

PUT zhtc
{
    "mappings": {
        "_doc": {
            "properties": {
                "content": {
                    "analyzer": "ik_max_word",
                    "type": "text"
                },
                "ctime": {
                    "type": "date"
                },
                "gather": {
                    "properties": {
                        "site_domain": {
                            "type": "keyword"
                        },
//检索site_name中的内容进行分词(类型为text),不可以使用term query;但是检索site_name.keyword时,同样是对site_name中的内容,则不进行分词(可以使用term query)
                        "site_name": {
                            "fields": {
                                "keyword": {
                                    "type": "keyword"
                                }
                            },
                            "type": "text"
                        }
                    }
                },
                "like_count": {
                    "type": "integer"
                },
                "music_id": {
                    "type": "keyword"
                },
                "pic_urls": {
                    "type": "text"
                },
                "retweeted": {
                    "type": "object"
                },
                "title": {
                    "analyzer": "ik_max_word",
                    "type": "text"
                }
            }
        }
    },
    "settings": {
        "index": {
            "number_of_replicas": 1,
            "number_of_shards": 5
        }
    }
}

二、更新

  1. 更新查询后的内容

    更新id为1或2的数据,flag为2

    {
        "script":{
            "lang":"painless",
            "source":"ctx._source.flag=params.flag",
            "params":{
                "flag":2
            }
        },
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "id": [
                                "1",
                                "2"
                            ]
                        }
                    }
                ]
            }
        }
    }
    
  2. 更新特定数据某个字段

    post http://localhost:9200/{index}/{type}/{_id}/_update
    
    {
        "doc": {
            "create_time": "2021-09-02 08:23:04",
            "update_time": "2021-09-02 08:23:04"
        }
    }
    

五、删除

  1. 删除掉查询到的内容

    post http://localhost:9200/{index}/_delete_by_query
    
    {
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "id": [
                                "1"
                            ]
                        }
                    }
                ]
            }
        }
    }
    

六、参数设置

  1. 修改默认的更新时间

    put http://localhost:9200/{index}/_settings
    {
      "index":{
        "refresh_interval":"1s"
      }
    }
    
posted @ 2020-11-10 19:14  ayueC  阅读(131)  评论(0)    收藏  举报