zoukankan      html  css  js  c++  java
  • elasticsearch(二)

    文章借鉴,原文链接https://www.dubby.cn/detail.html?id=9077

    基础语法

    1、使用工具

      postman

    2、索引创建

      PUT http://127.0.0.1:9200/people/

    {
      "settings":{
          "number_of_shards":3,//分片数,默认为5
          "number_of_replicas":1 //备份数
      },
    //settings可以不设置,默认即可
     //构建结构化索引,映射 "mappings":{ "person":{ //结构类型 "properties":{//设置属性 "name":{ "type":"text" //数据类型 }, "age":{ "type":"integer" }, "sex":{ "type":"text" }, "birthday":{ "type":"date", "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis" //
    epoch_millis 时间戳
              },
              "country":{
                "type":"keyword"
              }
            }
          }
        }
    }

    3、插入数据

      PUT http://127.0.0.1:9200/people/person/1

    {
      "name":"lyc",
      "sex":"男",
      "birthday":"1993-01-20",
      "country":"中国",
      "age":20
     }

    这个URI后面的1代表的是这条数据的ID,也可以字符串。如果不想自己指定ID,可以不传,但是必须使用POST来新增,这样的话Elasticsearch会给这条数据生成一个随机的字符串。

    如果想对这条数据进行更新,可以重新请求这个URI,关键是这个ID要指定,然后修改json内容,这样就可以更新这条数据了。

    4、检索数据

    根据ID检索某条具体数据:GET http://127.0.0.1:9200/people/person/1

    查询效果:

    {
        "_index": "people",
        "_type": "person",
        "_id": "1",
        "_version": 1,
        "found": true,
        "_source": {
            "name": "lyc",
            "sex": "男",
            "birthday": "1993-01-20",
            "country": "中国",
            "age": 20
        }
    }

    其中 _source就是我们存储的数据信息,若将请求方法GET改为DELETE,则删除这条数据

    5、最简单的搜索

    GET http://127.0.0.1:9200/people/_search

    结果:

    {
        "took": 9,
        "timed_out": false,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 2,
            "max_score": 1,
            "hits": [
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "name": "lyc",
                        "sex": "男",
                        "birthday": "1993-01-20",
                        "country": "中国",
                        "age": 18
                    }
                },
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                        "name": "nini",
                        "sex": "女",
                        "birthday": "1995-01-20",
                        "country": "中国",
                        "age": 18
                    }
                }
            ]
        }
    }

    搜索指定Index下的Type的全部文档,默认每页只显示10条,可以通过size字段改变这个设置,还可以通过from字段,指定位移(默认是从位置0开始)。返回结果的 took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录

    6、简单条件搜索

    查询name为lyc的数据

    GET http://127.0.0.1:9200/people/_search?q=name:lyc

    7、条件搜索

    POST http://127.0.0.1:9200/people/_search

    参数:

    {
        "query" : {
            "match" : {
                "name" : "nini"
            }
        }
    }

    结果:

    {
        "took": 9,
        "timed_out": false,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 1,
            "max_score": 0.2876821,
            "hits": [
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "1",
                    "_score": 0.2876821,
                    "_source": {
                        "name": "nini",
                        "sex": "女",
                        "birthday": "1995-01-20",
                        "country": "中国",
                        "age": 18
                    }
                }
            ]
        }
    }

    这段查询和上面的例子是一样的,不过参数从简单的参数变成了一个复杂的json,不过复杂带来的优势就是控制力更强,我们可以对查询做出更多精细的控制。

    8、更复杂的搜索

    POST http://127.0.0.1:9200/people/_search

    {
        "query" : {
            "bool": {
                "must": {
                    "match" : {
                        "name" : "nini" 
                    }
                },
                "filter": {
                    "range" : {
                        "age" : { "gt" : 10 } 
                    }
                }
            }
        }
    }

    这里新增了一个range过滤器,gt 表示_大于(_great than)

    9、全文搜索

    POST http://127.0.0.1:9200/people/_search

    {
        "query" : {
            "match" : {
                "name" : "nini lyc"
            }
        }
    }

     搜索name中包含nini 或 lyc字眼的数据,若是精确匹配,则将match改为match_phrase

    10、高亮搜索

    POST http://127.0.0.1:9200/people/_search

    {
        "query" : {
            "match" : {
                "name" : "nini lyc"
            }
        },
        "highlight": {
            "fields" : {
                "name" : {}
            }
        }
    }

     结果:

    {
        "took": 96,
        "timed_out": false,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 2,
            "max_score": 0.2876821,
            "hits": [
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "2",
                    "_score": 0.2876821,
                    "_source": {
                        "name": "lyc",
                        "sex": "男",
                        "birthday": "1993-01-20",
                        "country": "中国",
                        "age": 18
                    },
                    "highlight": {
                        "name": [
                            "<em>lyc</em>"
                        ]
                    }
                },
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "1",
                    "_score": 0.2876821,
                    "_source": {
                        "name": "nini",
                        "sex": "女",
                        "birthday": "1995-01-20",
                        "country": "中国",
                        "age": 18
                    },
                    "highlight": {
                        "name": [
                            "<em>nini</em>"
                        ]
                    }
                }
            ]
        }
    }

    返回结果多了个highlight的部分,默认是用<em></em>包裹

    11、简单聚合

    在聚合之前,需要做些修改,因为Elasticsearch默认是不支持对text类型的数据聚合的,所以需要先开启"fielddata": true

    PUT http://127.0.0.1:9200/people/

    {
      "settings":{
          "number_of_shards":3,
          "number_of_replicas":1
      },
      "mappings":{
            "person":{
                "properties":{
                    "name":{
                        "fielddata": true,
                        "type":"text"
                    },
                    "age":{
                        "type":"integer"
                    },
                    "sex":{
                        "type":"text"
                    },
                    "birthday":{
                        "type":"date",
                        "format":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
                    },
                    "country":{
                        "type":"keyword"
                    }
                } 
            }
            
        }
     }

    按照name聚合

    POST http://127.0.0.1:9200/people/_search

    {
      "aggs": {
        "all_interests": {
          "terms": { "field": "name" }
        }
      }
    }

    结果:

    {
        "took": 25,
        "timed_out": false,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 3,
            "max_score": 1,
            "hits": [
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "name": "liyingchun",
                        "sex": "男",
                        "birthday": "1993-01-20",
                        "country": "中国",
                        "age": 20
                    }
                },
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                        "name": "lyc",
                        "sex": "男",
                        "birthday": "1993-01-20",
                        "country": "中国",
                        "age": 20
                    }
                },
                {
                    "_index": "people",
                    "_type": "person",
                    "_id": "3",
                    "_score": 1,
                    "_source": {
                        "aggs": {
                            "all_interests": {
                                "terms": {
                                    "field": "name"
                                }
                            }
                        }
                    }
                }
            ]
        },
        "aggregations": {
            "all_interests": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "liyingchun",
                        "doc_count": 1
                    },
                    {
                        "key": "lyc",
                        "doc_count": 1
                    }
                ]
            }
        }
    }

     过滤后聚合

    {
        "query": {
        "match": {
          "name": "lyc"
        }
      },
      "aggs": {
        "all_interests": {
          "terms": { "field": "name" }
        }
      }
    }

    结果:

    {
        "_index": "people",
        "_type": "person",
        "_id": "3",
        "_version": 4,
        "result": "deleted",
        "_shards": {
            "total": 2,
            "successful": 2,
            "failed": 0
        },
        "_seq_no": 8,
        "_primary_term": 1
    }

    12、计数

    POST http://127.0.0.1:9200/_count

    {
        "query": {
            "match_all": {}
        }
    }

    结果:

    {
        "count": 2,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        }
    }

    对某个type计数

    POST http://127.0.0.1:9200/people/person

    {
        "query": {
            "name" : "lyc"
        }
    }

    结果:

    {
        "_index": "people",
        "_type": "person",
        "_id": "9-31KGYBiQSwQuaX7D-s",
        "_version": 1,
        "result": "created",
        "_shards": {
            "total": 2,
            "successful": 2,
            "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1
    }

    13、集群健康

    GET http://127.0.0.1:9200/_cluster/health

    结果:

    {
        "cluster_name": "liyingchun",
        "status": "green",
        "timed_out": false,
        "number_of_nodes": 2,
        "number_of_data_nodes": 2,
        "active_primary_shards": 3,
        "active_shards": 6,
        "relocating_shards": 0,
        "initializing_shards": 0,
        "unassigned_shards": 0,
        "delayed_unassigned_shards": 0,
        "number_of_pending_tasks": 0,
        "number_of_in_flight_fetch": 0,
        "task_max_waiting_in_queue_millis": 0,
        "active_shards_percent_as_number": 100
    }

    14、监控单个节点

    GET http://127.0.0.1:9200/_nodes/stats

    15、集群统计

    GET http://127.0.0.1:9200/_cluster/stats

  • 相关阅读:
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    Python for Data Science
    软件工程实践总结
  • 原文地址:https://www.cnblogs.com/liyingchun/p/9728996.html
Copyright © 2011-2022 走看看