全文檢索 — ElasticSearch_04(ES的常用CURL命令、更改索引已有字段的數據類型、查詢結果超過10000報500錯誤)


歡迎訪問筆者個人技術博客:http://rukihuang.xyz/

1 基本語法

curl [ -s][ -g][ -X<REST Verb>][ -H 'Content-Type: application/json'] '<Node>:<Port>/<Index>[/Type][/ID]/_search?pretty&q=<search string>'
  • -s:不輸出查詢的時間等信息
  • -g:轉義用
  • <REST Verb>:REST請求的,get/post/put/delete
  • <Node>:節點ip,本機爲localhost
  • <Port>:階段端口,es爲9200
  • <index>:索引名,支持通配符*
  • <Type>:索引類型,一個index只有1個type,可不輸入
  • <ID>:操作對象的ID號,可不輸入
  • q:前面加&,後面加查詢語句

2 常用參數

參數 作用 備註
q 查詢字符串
s(sort) 排序
from 從命中的hits開始返回 默認爲0
size 返回的hits數量 默認爲10
_source_include 查詢包含某些source字段的文檔
_source_exclude 查詢不包含某些source字段的文檔
timeout 搜索超時,將在指定執行時間內查到的hits返回 默認無超時
default_field 指定字段,未指定字段前綴時返回所有字段 默認爲index.query.default_field
default_operator 指定查詢運算符 未指定,默認爲or
analyzer 指定用於分析查詢字符串的分析器
_source 布爾設定是否使用_source字段檢索 false禁用
analyze_wildcard 布爾設定是否模糊查詢(通配符、前綴) 默認false禁用
pretty json 默認爲true
查詢字符串q=[..] _exists_:title 是否存在
status:active 查詢status字段是active的文檔
title:(quick OR brown) 查詢title字段是quickbrown的文檔
author:“John Smith” 查詢author字段是John Smith的文檔,因爲有空格,所以要用引號包起來
date:[2012-01-01 TO 2012-12-31] 查詢date字段是active的文檔
count:[10 TO *] 查詢count字段從10開始(增長)的文檔
count:>=10 查詢count字段大於等於10的文檔

3 常用CURL語句

3.1 查看node狀態

curl localhost:9200/_cat/nodes?v

3.2 查看集羣健康狀況

curl localhost:9200/_cat/health?v

3.3 查看全部索引(排序後)

curl 'localhost:9200/_cat/indices?v&s=index'

3.4 查看特定索引

curl 'localhost:9200/_cat/indices/dayapi*?v&s=index' 
  • *:通配符

3.5 pretty,美觀

curl 'localhost:9200/dayapi*?pretty'

3.6 是否存在

curl 'localhost:9200/dayapi*/_search?pretty&q=_exists_:MULT'

3.7 查指定的字段值

curl 'localhost:9200/dayapi*/_search?pretty&q=TESTID:123'

3.8 查指定的字段值,並只顯示3個

curl 'localhost:9200/dayapi*/_search?pretty&q=TESTID:123&size=3'

3.9 從第3個開始只顯示3個

curl 'localhost:9200/dayapi*/_search?pretty&q=TESTID:123&from=2&size=3'

3.10 按時間排序,desc降序,默認爲升序

curl 'localhost:9200/dayapi*/_search?pretty&q=TESTID:123&sort=TIME:desc'

3.11 模糊查詢

curl 'localhost:9200/dayapi*/_search?pretty&analyze_wildcard&q=TESTID:123'

3.12 比較大小

curl 'localhost:9200/dayapi*/_search?pretty&q=VAL:<200'

3.13 是否顯示

curl 'localhost:9200/dayapi*/_search?pretty&_source=false'

3.14 設置包含的字段

curl 'localhost:9200/dayapi*/_search?pretty&_source_includes=TIME,VAL'

3.15 組合查詢

curl -g 'localhost:9200/dayapi*/_search?pretty&q=(SOLAR:1%20AND%20CENTRAL:1)'
  • AND一定要大寫,不然識別不出
  • 組合條件之間用%20轉義後的空格分隔
  • 括號內的條件用字段名:值表示

3.16 範圍查詢

curl -g 'localhost:9200/dayapi*/_search?pretty&q=TIME:[2019-05%20TO%202019-06]'
  • 範圍查詢條件字段必須爲可比較的字段類型,如date,integer,double

3.17 關閉打開索引

curl -XPOST localhost:19200/dayapi*/_close
curl -XPOST localhost:19200/dayapi*/_open

3.18 刪除符合條件的記錄

curl -XPOST 'localhost:19200/dayapi*/_delete_by_query?pretty&q=TESTID:781128

3.19 新建索引並設定mapping

curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/dayapi2/' -d '
{
    "mappings" : {
      "market_api" : {
        "properties" : {
          "prop_1" : {
            "type" : "keyword"
          },
          "prop_2" : {
            "type" : "double"
          },
          "prop_3" : {
            "type" : "keyword"
          },
          "prop_4" : {
            "type" : "integer"
          }
        }
      }
    }
}'

3.20 刪除索引

curl -XDELETE 'http://localhost:9200/dayapi'

3.21 將’索引1’的數據轉到’索引2’ reindex

curl -H "Content-Type: application/json" -XPOST 'http://localhost:9200/_reindex' -d '
{
  "source": {
    "index": "index1"
  },
  "dest": {
    "index": "index2"
  }
}'

3.22 設置別名

curl -XPOST 'localhost:9200/_aliases' -d '{"actions": [{"add": {"index": "dayapi", "alias": "dayapi123"}}]}

3.23 查看某個索引的mapping

curl -XGET "http://localhost:9200/dayapi/_mapping?pretty"

4 遇到的問題

4.1 更改索引已有字段的類型

  • 思路來源於:https://blog.csdn.net/apple9005/article/details/90415558
    • 新建索引2
    • 給索引2的mapping設定字段類型
    • 索引1的數據 -> 索引2
    • 刪除索引1(舊),新建新的索引1(索引名一致),設置新的mapping字段類型
    • 索引2的數據 -> 索引1(新)
    • 刪除索引2
  • 相當於左手倒右手,左手穿上手套後,右手倒左手

4.2 es查詢結果大於10000,報500

curl -H "Content-Type: application/json" -XPUT localhost:9200/dayapi/_settings -d '{ "index.max_result_window" :"1000000"}'

參考文章
https://www.cnblogs.com/daynote/p/11076965.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章