ElasticSearch相关基础汇总:    1、版本演变:  1.x----->2.x----->5.x----->6.x     2、mac电脑上安装elasticsearch命令: brew  install  elasticsearch 或者直接下载压缩包解压    3、mac上查看已经安装好的services列表: brew  services list     4、实用插件head的安装:          (1)、下载:git clone git://github.com/mobz/elasticsearch-head.git          (2)、cd elasticsearch-head          (3)、npm install  或者  cnpm install          (4)、npm run start 或者 cnpm run start    5、配置elasticsaerch 和 elasticsearch-head 插件          (1)、停掉elasticsearch-head           (2)、停掉elasticsearch:    brew services stop elasticsearch          (3)、查找elasticsearch.yml文件位置:     sudo find / -name elasticsearch.yml          (4)、在elasticsearch.yml文件末尾添加:                        http.cors.enabled: true                    http.cors.allow-origin:  "*"                              (5)、 启动es:  brew services start  elasticsearch  启动elasticsearch-head:  cnpm run start                         6、分布式安装es ,一个master节点,两个slave节点          (1)、修改主节点elasticsearch.yml配置文件,在文件末尾添加:                   cluster.name:  biges         #指定es集群的名字                   node.name:   master          #指定节点的名字                   node.master:  true           #指定当前节点就是主节点                   network.host:  127.0.0.1     #指定为本机地址,端口还是9200                            (2)、修改slave节点的  elasticsearch.yml 文件:                   cluster.name:  biges         #指定es集群的名字                       node.name:   slave1/slave2   #指定节点的名字                   network.host:  127.0.0.1     #指定为本机地址                   http.port: 9201/9202         #指定slave节点的端口                      discovery.zen.ping.unicast.hosts: ["127.0.0.1"]     #发现master位置                                 7、es基础概念          (1)、集群与节点:   master + slave1 + slave2  总体是一个集群,master、slave1、slave2分别是一个子节点          (2)、索引:含有相同属性的文档的集合          (3)、类型:索引可以定义一个或者多个类型,文档必须属于一个类型          (4)、文档:文档是可以被索引的基本数据单位          (5)、分片:每个索引都有多个分片,每个分片是一个lucene索引          (6)、备份:拷贝一份分片就完成了分片的备份              8.es中索引相关:          (1)、API基本格式: http://<ip>:<port>/<索引>/<类型>/<文档id>          (2)、restful  api 的 http 动词: get/put/post/delete          (3)、实用postman创建索引很方便。    9.es的查询语法          (1)、简单查询:               127.0.0.1:9200/book/novel/1                    (2)、条件查询               127.0.0.1:9200/book/_search               {                  "query":{                      #"match_all": {}                      "match":{                          " ": "elastcisearch"                      }                  },                  "from": 1,                  "size": 10,                  "sort": [                        {"publish_date": {"order": "desc"}}                  ]               }                    (3)、聚合查询                127.0.0.1:9200/book/_search                                {                    "aggs":{                         "group_by_word_count": {                              "terms": {                                  "field": "word_count"                              }                        },                                                "group_by_publish_date": {                              "terms": {                                  "field": "publish_date"                              }                        }                    }                }                                或者                                 {                    "aggs":{                         "group_word_count": {                              "stats": {                                  "field": "word_count"                              }                        }                    }                }                                  10、高级查询       子条件查询:  特定字段查询所指特定值       复合条件查询:  以一定的逻辑组合子条件查询            (1)、Query            全文本查询:  针对文本类型数据            字段级别查询:   针对结构化数据,如数字、日期等            模糊匹配:                127.0.0.1:9200/book/_search                {                    "query": {                        "match": {                             "author"; "test"                        }                    }                }                或者                {                    "query": {                        "match_phrase": {                             " "; "elasticsearch入门"                        }                    }                }                多字段匹配查询:                {                    "query":{                        "multi_match": {                            "query": "瓦利",                            "fields":  ["author" ," "]                        }                    }                }                语法查询:                {                    "query": {                        "query_string": {                            #"query": "(elasticsearch AND  大法) OR python"                            "query":  "test  OR elasticsearch",                            "fields":  [" ","author"]                        }                    }                }                结构化查询:                {                    "query":{                        "term":{                            #"word_count": 1000                            "author":  "test"                        }                    }                }                范围查询:                {                    "query": {                        "range": {                            "word_count": {                                "gte": 1000,                                "lte": 10000                            }                        }                    }                }                                                            (2)、filter            在查询的过程中,只判断该文档是否满足条件,只有yes或者no            127.0.0.1:9200/book/_search            {                "query": {                    "bool": {                        "filter": {                            "term":  {                                "word_count":  1000                            }                        }                    }                }            }      (3)、复合查询            固定分数查询 , 只支持filter            127.0.0.1:9200/_search            {                "query": {                    "constant_score": {                         "filter": {                            "match": {                                " ": "elasticsearch"                            }                        },                        "boost": 2                    }                }            }                        布尔查询            {                "query": {                    "bool": {                        "should": [                            {                                "match": {                                    "author": "test"                                }                            },                            {                                "match":  {                                    " ": "elasticsearch"                                }                            }                        ]                    }                }            }                        或者:                        {                "query": {                    "bool": {                        "must": [                            {                                "match": {                                    "author": "test"                                }                            },                            {                                "match": {                                    " ": "elasticsearch"                                }                            }                        ],                        "filter":[                            {                                "term": {                                    "word_count": 1000                                }                            }                        ]                    }                }            }            或            {                "query": {                    "bool": {                        "must_not": {                            "term": {                                "author": "test"                            }                        }                    }                }            }                                    备注:本文是从个人的印象笔记之中整理到博客文章的,印象笔记写博客,整理文章还是欠缺,还得是博客才行,后期会保持每天一篇的节奏将个人印象笔记中记录的东西整理到阿里云博客上,阿里云博客上传图片有时候会显示不出来,尴尬。       
收藏 打印