zoukankan      html  css  js  c++  java
  • Elasticsearch基本操作

    ElasticSearch操作说明

     

    活动

    方法

    url

    Body

     

    集群的相关操作

     

    查看健康

    Get

    http://localhost:9200/_cluster/health

     

     

    查看节点

    Get

    http://localhost:9200/_cluster/state/nodes

     

     

    关闭节点

    Post

    http://localhost:9200/_cluster/nodes/节点名字/_shutdown

    注意:每次启动后,节点名字都会改变

    关闭集群

    post

    http://localhost:9200/_cluster/nodes/_shutdown

     

     

     

    索引的相关操作

     

    创建索引settings

    Post

    http://localhost:9200/testindex

     

    {}

    创建索引settings

    Post

    http://localhost:9200/testindex/

     

     

    {

        "settings": {

          "index": {

            "number_of_shards":   "5",

            "number_of_replicas":   "0"

          }      }}

    修改一个索引setting

    Put

    不能post

    http://localhost:9200/testindex/_settings

     

    {

        "settings": {

          "index": {

            "number_of_replicas":   "0"

          }      }}

    删除索引setting

    Del

    http://localhost:9200/testindex/

     

     

    删除所有索引setting

    Del

    http://localhost:9200/_all/

    http://localhost:9200/*/

     

    查看具体索引setting

    Get

    http://localhost:9200/testindex/_settings

     

    关闭索引

    Post

    http://localhost:9200/testindex/_close

     

    打开索引

    post

    http://localhost:9200/testindex/_open

     

    查看所有索引setting

    Get

    http://localhost:9200/_settings

     

     

     

    添加一个type

    添加/修改一个mapping

    Put/

    post

    http://localhost:9200/testindex/testtype/_mapping

    {

          "testtype"   : {

            "properties" : {

              "address" : {

                "type" :   "string"

              },

              "name" : {

                "type" :   "string"

              }        }        }}

    删除mapping

    del

    http://localhost:9200/testindex/testtype

     

     

    添加具体的文档

    插入文档自动生成id

    Post

    http://localhost:9200/testindex/testtype/

    post每次都是创建新的

    {

        "name":"张明",

        "address":"中山北路"

    }

    插入文档,更新或生成

    put

    http://localhost:9200/testindex/testtype/1

    put时带具体的ID,表示修改,没有则创建

     

    {

        "name":"张明",

        "address":"中山北路"

    }

    普通查询

    Get

    http://localhost:9200/testindex/testtype/1

    {

        "name":"张明",

        "address":"中山北路"

    }

    Query查询

    Get

    http://localhost:9200/testindex/testtype/_search?q=name:张明

    {

        "name":"张明",

        "address":"中山北路"

    }

     

     

     

     

    其他操作:

    插入文档自动生成index/type

    Post

    http://localhost:9200/testindex/testtype/

    post每次都是创建新的

    {

        "name":"张明",

        "address":"中山北路"

    }

    插入文档自动生成index/type

    put

    http://localhost:9200/testindex/testtype/1

    put时带具体的ID,表示修改,没有则创建

     

    {

        "name":"张明",

        "address":"中山北路"

    }

    同时创建index、type,不插入文档

    Post/

    put

    http://localhost:9200/testindex

     

    后面的json格式最好不要变动,一般情况下,只要post过去的内容和查询看到的格式保持一个层次结构,都可以提交;

    但是提交的格式却不一定和查询看到的一致(毕竟提交只是需要将所有的信息加上去就可以了)

    {  

        "settings" : {

            "number_of_shards" :   "5",

            "number_of_replicas" :   "1"

        }      ,

            "mappings" : {

          "testtype" : {

            "properties" : {

              "address" : {

                "type" :   "string"

              },

              "age" : {

                "type" :   "string"

              }

            }

          }

          }

        }

    刷新索引

    Post

    http://localhost:9200/testindex/_refresh

     

    提交索引

    Post

    http://localhost:9200/testindex/_flush

     

    优化索引

    Post

    http://localhost:9200/testindex/_optimize

     

    清空缓存

    Post

    http://localhost:9200/testindex/_cache/clear

     

     

     

    查询操作:

    Math_all

    全集查询

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    http://172.16.20.49:9200/*/zuheentity/_search

    http://172.16.20.49:9200/*/_search

    http://172.16.20.49:9200/_search

     

    {

     }

    Math

    语义查询

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    http://172.16.20.49:9200/*/zuheentity/_search

    http://172.16.20.49:9200/*/_search

    http://172.16.20.49:9200/_search

     

    {

        "query":{

            "match":{           

                "ManagerName":"财富"

            }

        }

    }

    Math_phrase

    短语查询,term查询后,进行位置匹配,得到类似like结果

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    http://172.16.20.49:9200/*/zuheentity/_search

    http://172.16.20.49:9200/*/_search

    http://172.16.20.49:9200/_search

     

    {

        "query":{

            "match":{           

                "ManagerName":"财富"

            }

        }

    }

    Term

    直接用倒排索引的词项进行查询,性能好,但在应用层面,直接使用的少

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

     

    {

        "query":{

            "term":{           

                "ManagerName":{

                    "value":"财",

                    "boost":10

                }

            }

        }

    }

    Wildcard

    在term查询的基础上,增加通配符功能

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    {

        "query":{

            "wildcard":{           

                "ManagerName":{

                      "value":"?*"

                   ,

                    "boost":10

                }

            }

        }

    }

    Terms

    最低匹配查询已不再支持,需要使用bool查询

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    {

        "query":{

            "terms":{           

                "ManagerName":["富","宇","财","富"]

                 ,"minimum_match": 1

            }

        }

    }

    query_string

    +-前面注意要有一个空格

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    {

        "query":{

            "query_string":{

                "query":"   +ManagerName:财富广场 -ManagerName:广场"

            }

        }

    }

     

    过滤器

    Filter

    查询

    Filter查询不同时支持bool、or、and、exists、missing查询

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    {  "filter":

            {

                "bool":

                {"should":

                      [{"term":{"ManagerName":"世"}}

                       ,{"term":{"ManagerName":"界"}}

                    ],

                "must_not":

                    {"term":

                          {"ManagerName":"水"}

                    }

                }

            }

    }

    Query

    语义查询

    Post

    http://172.16.20.49:9200/zuhe/zuheentity/_search

    {  "query":

            {

                "bool":

                {"should":

                      [{"term":{"ManagerName":"世"}}

                       ,{"term":{"ManagerName":"界"}}

                    ],

                "must_not":

                    {"term":

                          {"ManagerName":"水"}

                    }

                }

            }

    }

    Filter、query联合查询

     

     

    {  "filter":

            {

                "bool":

                {"should":

                    [{"term":{"ManagerName":"世"}}

                       ,{"term":{"ManagerName":"界"}}

                    ],

                "must":

                    {"term":

                          {"ManagerName":"水"}

                    }

                }

            }

            ,"query":

            {

                "bool":

                {"should":

                      [{"term":{"ManagerName":"世"}}

                       ,{"term":{"ManagerName":"界"}}

                    ],

                "must_not":

                    {"term":

                          {"ManagerName":"界"}

                    }

                }

            }

    }

     

    高级查询:

    首先建立mapping关系,表示在type1对应的文档中,包含一个obj1的对象;

    {

        "type1" : {

            "properties" : {

                "obj1" : {

                    "type" : "nested"

                }

            }

        }

    }

    嵌套查询,

    {

        "query":{

        "nested" : {

            "path" : "obj1",

            "query" : {

                "bool" : {

                    "must" : [

                        {

                            "match" : {"obj1.name" : "善亮"}

                        }

                    ]

                }

            }

        }

    }

    }

    父查询、子查询同样,先建立mapping关系;

    Put

    http://172.16.20.49:9200/testindex/

    “”{

      "mappings": {

        "grouptype": {

           

            "properties" : {

              "groupid" : {

                "type" : "long"

              }

               

            }

            },

        "person": {

          "_parent": {

            "type": "grouptype"

          }

        }

      }

    }

    插入元素:

    http://172.16.20.49:9200/testindex/person/1?parent=1

    {

       

        "name":"zhan san",

        "age":"10"

    }

    查询父:

    http://172.16.20.49:9200/testindex/grouptype/_search

    {

      "query": {

        "has_child": {

          "type": "person",

          "query": {

            "match": {

              "age": 10

            }

          }

        }

      }

    }

    https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html

    查询子:

    http://172.16.20.49:9200/testindex/person/_search

    {

      "query": {

        "has_parent": {

          "type": "grouptype",

          "query": {

            "match": {

              "groupname": "搜索组"

            }

          }

        }

      }

    }

     分组统计:

    {
      "query": {
        "filtered": {
          "filter": {
            "missing": {
              "field": "Zhlx"
            }
          }
        }
      },
      "size": 1,
      "aggregations": {
        "tag": {
          "terms": {
            "field": "Permit",
            "size": 20
          }
        }
      }
    }
    View Code

     post

     批量插入:

    { "index": { "_index": "user","_type": "userinfo","_id": "6321113681837248" } }
    {"uid":"6321113681837248","sid":"10000017","displayname":"ZYH","level":"3","introduce":"Good Luck"}
    { "index": { "_index": "user","_type": "userinfo","_id": "1871112725585910" } }
    {"uid":"1871112725585910","level":"4"}
    { "index": { "_index": "user","_type": "userinfo","_id": "5123094091554478" } }
    {"uid":"5123094091554478","level":"24"}
    { "index": { "_index": "user","_type": "userinfo","_id": "7340313454673584" } }
    {"uid":"7340313454673584","level":"27"}
    { "index": { "_index": "user","_type": "userinfo","_id": "2465111535617940" } }
    {"uid":"2465111535617940","level":"25"}
    { "index": { "_index": "user","_type": "userinfo","_id": "7221114042211736" } }
    {"uid":"7221114042211736","level":"21"}
    { "index": { "_index": "user","_type": "userinfo","_id": "3006113720930996" } }

    post

    批量更新:

    { "update": { "_index": "user","_type": "userinfo","_id": "6321113681837248" }}
    {"doc":{"uid":"6321113681837248","sid":"10000017","displayname":"ZYH","level":"3","introduce":"Good Luck"},"upsert":{"uid":"6321113681837248","sid":"10000017","displayname":"ZYH","level":"3","introduce":"Good Luck"}}

    查看分词器效果:

    http://172.16.58.54:9200/_analyze?analyzer=standard&pretty=true&text=我是中国人"

    http://www.cnblogs.com/xing901022/p/5339419.html

  • 相关阅读:
    BZOJ_4383_[POI2015]Pustynia_线段树优化建图+拓扑排序
    BZOJ_1492_[NOI2007]货币兑换Cash_CDQ分治+斜率优化
    BZOJ_3073_[Pa2011]Journeys_线段树优化建图+BFS
    BZOJ_2726_[SDOI2012]任务安排_斜率优化+二分
    BZOJ_1406_[AHOI2007]密码箱_枚举+数学
    哈希表(Hash table)
    算法分析方法之平摊分析(Amotized Analysis)
    数据库视图功能的使用
    不基于比较的排序算法:Counting-sort和Radix-sort
    QuickSort(快速排序)的JAVA实现
  • 原文地址:https://www.cnblogs.com/thaughtZhao/p/5236249.html
Copyright © 2011-2022 走看看