Elasticsearch 筆記 (version:elasticsearch-5.5.x)

概念

Index

類似于關(guān)系型數(shù)據(jù)庫中的database。索引只是一個邏輯上的空間,物理上是分為多個文件來管理的。命名必須全小寫。

Type

類似于關(guān)系型數(shù)據(jù)庫中的table,根據(jù)用戶需求每個index中可以新建任意數(shù)量的type。

Document

類似于關(guān)系型數(shù)據(jù)庫中的row。每個Document是一個json格式的文本。

Mapping

更像是一個用來定義每個字段類型的語義規(guī)范在mysql中類似sql語句,在ES中經(jīng)過包裝后,都被封裝為友好的Restful風(fēng)格的接口進行操作。這一點也是為什么開發(fā)人員更愿意使用ES或者compass這樣的框架而不是直接使用Lucene的一個原因。

安裝

OS參數(shù)調(diào)整

vi /etc/security/limits.conf

* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096

vi /etc/sysctl.conf

vm.max_map_count=655360

ES參數(shù)調(diào)整

elasticsearch-2.4.5/config/jvm.options

-Xms512m
-Xmx512m

elasticsearch-2.4.5/config/elasticsearch.yml

network.host: 0.0.0.0
http.port: 9200

elasticsearch-analysis-ik安裝

下載版本:elasticsearch-analysis-ik-1.10.6.zip

下載地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

上傳elasticsearch-analysis-ik-1.10.6.zip至elasticsearch-2.4.5/plugins/analysis-ik(需先創(chuàng)建analysis-ik目錄)

解壓并修改plugin-descriptor.properties

java.version=1.8
elasticsearch.version=2.4.5

啟動

elasticsearch-2.4.5/bin/elasticsearch

驗證

GET http://{host}:9200

常見REST API 操作

查看集群狀態(tài)

GET http://{host}:9200/_cat/health?v

查看所有節(jié)點狀態(tài)

GET http://{host}:9200/_cat/nodes?v

創(chuàng)建索引

PUT http://{host}:9200/test_index

創(chuàng)建索引并指定配置

PUT http://{host}:9200/test_index

{
  "settings": {
     "refresh_interval": "1s",
     "number_of_shards" :   5,
     "number_of_replicas" : 0
  },
  "mappings": {
    "test_type": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "ik"
        },
        "about": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "ik"
        },
        "interests": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "ik"
        }
      }
    }
  }
}

修改索引設(shè)置

PUT http://10.16.30.37:9200/cpinfo_index/_settings

{
    "settings": {
         "number_of_replicas" : 1
    }
}

查看所有索引

GET http://{host}:9200/_cat/indices?v

查看索引mapping

GET http://{host}:9200/{index_name}/_mapping?pretty

向索引添加新type

POST http://{host}:9200/{index_name}/{type_name}

{
  "mappings": {
    "test_type2": {
      "dynamic": false,
      "properties": {
        "name": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "standard"
        },
        "about": {
          "type": "string",
          "index": "analyzed",
          "analyzer": "standard"
        }
      }
    }
  }
}

刪除索引

DELETE http://{host}:9200/{index_name}

添加數(shù)據(jù)

PUT http://{host}:9200/{index_name}/{type_name}/{id}

{
    "name" : "Scott",
    "about" :  "Smith",
    "location": {
    "lat":     40.722,
    "lon":    -73.989
  }
}

批量添加數(shù)據(jù)(方法一)

curl -XPOST localhost:9200/_bulk --data-binary @data.json

data.json內(nèi)容:
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "1" } }
{ "name" : "scott1", "about" : "work1",  "interests" : "read1"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "2" } }
{ "name" : "scott2", "about" : "work2",  "interests" : "read2"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "3" } }
{ "name" : "scott3", "about" : "work3",  "interests" : "read3"}

批量添加數(shù)據(jù)(方法二)

curl -XPOST http://127.0.0.1:9200/_bulk -d '
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "4" } }
{ "name" : "scott1", "about" : "work1",  "interests" : "read1"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "5" } }
{ "name" : "scott2", "about" : "work2",  "interests" : "read2"}
{ "create" : { "_index" : "test_index", "_type" : "test_type", "_id" : "6" } }
{ "name" : "scott3", "about" : "work3",  "interests" : "read3"}
'

查詢單條數(shù)據(jù)

GET http://{host}:9200/{index_name}/{type_name}/{id}?pretty

條件查詢

curl -XPOST 'http://{host}:9200/{index_name}/{type_name}/_search?pretty' -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "interests": "閱讀" } },
        { "match": { "age": 97 } }
      ]
    }
  },
  "sort": { "age": { "order": "desc" } },
  "from": 0,
  "size": 1000
}'

查看token

POST http://{host}9200/{index_name}/_analyze?analyzer=chinese

張三

查詢解析

POST http://{host}:9200/{index_name}/{type_name}/_validate/query?explain

{
  "query": {
    "multi_match": {
      "query": "學(xué)習(xí)",
       "fields": ["name", "about"]
    }
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Linux-Server-Notes PMS /home/softwareluke/圖片/2017-09-11 0...
    燕京博士閱讀 637評論 0 1
  • 最近項目用到了es搜索引擎,行業(yè)對全文搜索引擎方面對es具有高度的評價,es基于目前最流行的開源Luence封裝的...
    阿太哥閱讀 1,360評論 0 0
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,564評論 19 139
  • 烏云轉(zhuǎn)晴
    晴__空閱讀 235評論 0 0
  • 哀傷時候 我選擇沉默 如花在不是它的季節(jié)枯萎 臨近黑夜 抱著回憶入眠 眼皮的沉重 此刻也終于得到停歇 閉上眼睛 過...
    渝湘閱讀 286評論 6 7

友情鏈接更多精彩內(nèi)容