Elasticsearch 基礎入門教程

Elasticsearch基礎入門教程,經常使用的命令語句,可直接複製到kibana上使用,適用於6.x 7.x。

1 集羣/索引相關

1.1 查看集羣狀態

GET /_cat/health?v&pretty

1.2 查看集羣的索引狀態

GET /_cluster/health?pretty&level=indices

1.3 查看索引信息

GET /_cat/indices?v&pretty

1.4 查看分片信息

GET /_cat/shards?v&pretty

1.5 查看各節點的容量使用狀況

GET /_cat/allocation?v&pretty

1.6 查看各節點信息

GET /_nodes/process?pretty

GET /_cat/nodes?v

1.7 查看某個節點信息

GET /_nodes/node1/process?pretty

1.8 查看某個索引分片信息

GET /index/_search_shards

1.9 查看某個索引的fielddata

GET /_stats/fielddata?fields=*&index=xx

1.10 查看es分詞器的分詞結果

GET /_analyze/?pretty
{
  "analyzer": "ik_max_word",
  "text": "測試用例"
}

1.11 查看索引中某個文檔具體字段的分詞結果

GET /index/type/id/_termvectors?fields=title

1.12 設置搜索的最大返回數(三種方式)

1.12.1 全局配置

PUT /_settings
{
  "index": {
    "max_result_window": 100000000
  }
}

1.12.2 針對某個索引配置

PUT /index/_settings
{
  "index": {
    "max_result_window": 100000000
  }
}

1.12.3 配置文件

在config/elasticsearch.yml文件,加上index.max_result_window: 100000000node


2 搜索相關

2.1 match_all

匹配全部sql

GET /index/_search
{
  "query": {
    "match_all": {}
  }
}

2.2 match_phrase

短語匹配,要求全部的分詞必須同時出如今文檔中,同時位置必須緊鄰一致。數組

GET /index/_search
{
  "query": {
    "match_phrase": {
      "name": "quick brown fox"
    }
  }
}

2.3 match

match在匹配時會對所查找的關鍵詞進行分詞,而後按分詞匹配查找。
match會將關鍵詞進行分詞分紅「my」和「cat」,查找時包含其中任一都可被匹配到。app

GET /index/_search
{
  "query": {
    "match": {
      "name": "my cat"
    }
  },
  "sort": [
    {
      "age": "desc"
    }
  ]
}

2.4 multi_match

容許在match查詢的基礎上同時搜索多個字段,在多個字段中同時查一個。less

GET /index/_search
{
  "query": {
    "multi_match": {
      "query": "full text search",
      "fields": [
        "title",
        "body"
      ]
    }
  }
}

2.5 分頁查詢

from爲分頁偏移量,默認第一頁爲0,第n頁爲n*size。nosql

GET /index/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2
}

2.6 指定查詢結果的字段

GET /index/_search
{
  "query": {
    "match_all": {}
  },
  "_source": [
    "name",
    "age"
  ]
}

2.7 高亮顯示查詢結果

在每一個搜索結果中,高亮部分文本片斷,以便讓用戶知道爲什麼該文檔符合查詢條件。socket

GET /index/_search
{
  "query": {
    "match_phrase": {
      "name": "zhangsan"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

2.8 term與terms

term不分詞檢索,搜索不會對搜索詞進行分詞拆解,主要用於精確匹配哪些值。
terms容許指定多個匹配條件,多個term,相似於in查詢。elasticsearch

GET /index/_search
{
  "query": {
    "term": {
      "name": "xxx"
    }
  }
}

{
  "query": {
    "terms": {
      "tag": [
        "search",
        "full_text",
        "nosql"
      ]
    }
  }
}

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "tag": "search"
          }
        },
        {
          "term": {
            "tag": "nosql"
          }
        }
      ]
    }
  }
}

2.9 range過濾

gt:大於 gte:大於等於 lt:小於 lte:小於等於測試

GET /index/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lt": 30
      }
    }
  }
}

2.10 bool

bool 過濾能夠用來合併多個過濾條件查詢結果的布爾邏輯
must:多個查詢條件的徹底匹配,至關於 and。
must_not:多個查詢條件的相反匹配,至關於 not。
should:至少有一個查詢條件匹配, 至關於 or。
filter:必須匹配,根據過濾標準來排除或包含文檔。ui

GET /index/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "zhangsan"
        }
      },
      "filter": {
        "range": {
          "age": {
            "gt": 25
          }
        }
      }
    }
  }
}

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "IBM"
          }
        },
        {
          "match": {
            "cluster_category": "其餘文章"
          }
        }
      ]
    }
  }
}

2.11 多索引查詢

注意:最後一個索引請求體後面需換行

GET /_msearch
{"index":"index1"}
{"query" : {"match_all" : {}}}
{"index":"index2"}
{"query" : {"match_all" : {}}}

2.12 多索引查詢

注意:最後一個索引請求體後面需換行

GET /_msearch
{"index":"index1"}
{"query" : {"match_all" : {}}}
{"index":"index2"}
{"query" : {"match_all" : {}}}

2.13 搜索結果返回總數

GET /index/_search
{
    "track_total_hits": true,
    "query":{"match_all":{}}
}

3 刪除數據

  • 刪除索引數據只能使用:deletebyquery,相比刪除索引,deletebyquery刪除數據只是邏輯刪除;
  • 真正的刪除實際是段合併後的物理刪除分段,也就是deletebyquery後,有一段時間磁盤空間不降反升。
POST /index/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

4 reindex

reindex會將一個索引的數據複製到另外一個已存在的索引,可是並不會複製原索引的mapping(映射)、shard(分片)、replicas(副本)等配置信息,可用於數據遷移。

  • 參數:

size,可選,批量提交條數,能夠提升效率,建議每批提交5-15M的數據
type,可選,索引類型
query,可選,添加查詢來過濾文檔
sort,可選,排序
_source,可選,指定字段
remote,可選,es鏈接配置

POST _reindex
{
  "source": {
    "index": "index1",
    "size": 1000,
    "type": "tweet",
    "query": {
      "term": {
        "xx": "xx"
      }
    },
    "sort": {
      "date": "desc"
    },
    "_source": [
      "xx"
    ],
    "remote": {
      "host": "http://xxx.xxx.xxx:9200",
      "username": "xxx",
      "password": "xxx",
      "socket_timeout": "1m",
      "connect_timeout": "30s"
    }
  },
  "dest": {
    "index": "index2"
  }
}

5 Nested嵌套類型

嵌套對象將數組中的每一個對象索引爲單獨的隱藏文檔,這意味着能夠獨立於其餘對象查詢每一個嵌套對象。

5.1 Nested類型 刪除操做

POST /indexName/_update/id
{
 "script": {
    "lang": "painless",
    "source": "ctx._source.comments.removeIf(it -> it.name == 'John');"
  }
}

5.2 Nested類型 修改操做

POST /indexName/_update/id
{
  "script": {
    "source": "for(e in ctx._source.comments){if (e.name == 'steve') {e.age = 25; e.comment= 'good article...';}}" 
  }
}

5.3 Nested類型 查詢操做

POST /index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "comments",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "comments.name": "William"
                    }
                  },
                  {
                    "match": {
                      "comments.age": 34
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
相關文章
相關標籤/搜索