zoukankan      html  css  js  c++  java
  • Elasticsearch: 权威指南---基础入门

    1、查看
    方式:GET
    URL:http://10.10.6.225:9200/?pretty

    pretty
    在任意的查询字符串中增加pretty参数。会让Elasticsearch美化输出JSON结果以便更加容易阅读

    实测:
    http://10.10.6.225:9200/?pretty

    http://10.10.6.225:9200/
    效果是一样的。

    返回:
    {
    "name": "Uf8MPqK",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "w_SOItRIQBGpZ4UuMTRlDg",
    "version": {
    "number": "6.2.1",
    "build_hash": "7299dc3",
    "build_date": "2018-02-07T19:34:26.990113Z",
    "build_snapshot": false,
    "lucene_version": "7.2.1",
    "minimum_wire_compatibility_version": "5.6.0",
    "minimum_index_compatibility_version": "5.0.0"
    },
    "tagline": "You Know, for Search"
    }
    2、计算集群中文档的数量,我们可以用这个:
    方式:POST
    URL:http://10.10.6.225:9200/_count?pretty
    BODY:
    {
    "query": {
    "match_all": {}
    }
    }
    返回:
    {
    "count": 1505295,
    "_shards": {
    "total": 36,
    "successful": 36,
    "skipped": 0,
    "failed": 0
    }
    }
    3、简单查询
    URL:http://10.10.6.225:9200/megacorp/employee/_search?q=last_name:Smith
    方式:GET
    返回:
    {
    "took": 53,
    "timed_out": false,
    "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
    },
    "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
    {
    "_index": "megacorp",
    "_type": "employee",
    "_id": "2",
    "_score": 0.2876821,
    "_source": {
    "first_name": "Jane",
    "last_name": "Smith",
    "age": 32,
    "about": "I like to collect rock albums",
    "interests": [
    "music"
    ]
    }
    },
    {
    "_index": "megacorp",
    "_type": "employee",
    "_id": "1",
    "_score": 0.2876821,
    "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
    "sports",
    "music"
    ]
    }
    }
    ]
    }
    }
    也可以使用查询表达式:
    URL:http://10.10.6.225:9200/megacorp/employee/_search
    方式:POST
    BODY:
    {
    "query" : {
    "match" : {
    "last_name" : "Smith"
    }
    }
    }
    4、复杂搜索
    目标:同样搜索姓氏为 Smith 的雇员,但这次我们只需要年龄大于 30 的。查询需要稍作调整,使用过滤器 filter ,它支持高效地执行一个结构化查询。
    URL:http://10.10.6.225:9200/megacorp/employee/_search
    方式:POST
    BODY:
    {
    "query" : {
    "bool": {
    "must": {
    "match" : {
    "last_name" : "smith"
    }
    },
    "filter": {
    "range" : {
    "age" : { "gt" : 30 }
    }
    }
    }
    }
    }

    5、添加mapping
    方式:PUT
    URL: http://10.10.6.225:9200/megacorp/_mapping/employee/
    {
    "properties": {
    "interests": {
    "type": "text",
    "fielddata": true
    }
    }
    }
    或者:
    方式:POST
    URL:http://10.10.6.225:9200/megacorp/employee/
    {
    "mappings": {
    "_doc": {
    "properties": {
    "interests": {
    "type": "text",
    "fields": {
    "keyword": {
    "type": "keyword"
    }
    }
    }
    }
    }
    }
    }
    7、聚合
    URL:http://10.10.6.225:9200/megacorp/employee/_search
    方式:POST
    {
    "size":0,
    "aggs": {
    "all_interests": {
    "terms": { "field": "interests" }
    }
    }
    }

    叫Smith的聚合
    URL:http://10.10.6.225:9200/megacorp/employee/_search
    方式:POST

    {"size":0,
    "query": {
    "match": {
    "last_name": "smith"
    }
    },
    "aggs": {
    "all_interests": {
    "terms": {
    "field": "interests"
    }
    }
    }
    }
    聚合还支持分级汇总 。比如,查询特定兴趣爱好员工的平均年龄:
    URL:http://10.10.6.225:9200/megacorp/employee/_search
    方式:POST
    {
    "size":0,
    "aggs" : {
    "all_interests" : {
    "terms" : { "field" : "interests" },
    "aggs" : {
    "avg_age" : {
    "avg" : { "field" : "age" }
    }
    }
    }
    }
    }

    遗留问题:

    1、http://10.10.6.225:9200/megacorp/employee/_search  格式是megacorp+employee二层,而我们的是一层:http://10.10.6.225:9200/t_resource_info/_search,这肯定是有问题的,需要明天仔细讨论下。

  • 相关阅读:
    [luoguP1437] [HNOI2004]敲砖块(DP)
    [luoguP2073] 送花(set)
    [luoguP1783] 海滩防御(二分 || 最短路 || 最小生成树)
    [luoguP3068] [USACO13JAN]派对邀请函Party Invitations(stl大乱交)
    [luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)
    数组
    String
    学不会的JVM
    异常
    反射
  • 原文地址:https://www.cnblogs.com/littlehb/p/8491869.html
Copyright © 2011-2022 走看看