Elasticsearchノート-004-ドキュメントAPI-CRUD-単一ドキュメント検索操作

6015 ワード

[toc]

単一ドキュメントget API


1.Get基本クエリー

GET weibo/_doc/1

output:
{
  "_index" : "weibo",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "niewj",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
  }
}

2.headクエリードキュメントが存在するかどうか

GET weibo/_doc/2結果:
{
  "_index" : "weibo",
  "_type" : "_doc",
  "_id" : "2",
  "found" : false
}

id=2のドキュメントが存在しないことがわかります.HEAD weibo/_doc/2結果:404 - Not Found文書id=1は存在する:GET weibo/_doc/1の結果:
{
  "_index" : "weibo",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "niewj",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
  }
}
HEADで照会:HEAD weibo/_doc/1、結果:200 - OKHEAD:調べられない:404;検出:200;

3.getクエリーインデックスのリアルタイム性


ドキュメントが更新されたが、まだリフレッシュされていない場合、get APIは、ドキュメントを表示するためのリフレッシュコールを発行します.デフォルトでは、get APIはリアルタイムであり、インデックスのリフレッシュレートの影響を受けない.(もちろんrealtime GETを無効にすることもでき、realtimeパラメータをfalseに設定することもできます.)

4. \ _ソースの無効化


デフォルトでは、get操作は無効にしない限りsourceコンテンツを返します.ソースフィールド:GET weibo/_doc/1 :
{
  "_index" : "weibo",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "niewj",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
  }
}

無効:GET weibo/_doc/1?_source=false:
{
  "_index" : "weibo",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true
}

5. \_sourceフィールドのフィルタリング(excludes/includes)


ソースの1つまたは2つのフィールドのみを使用する場合は、_source_includeパラメータと_source_excludeパラメータを使用して、次のパラメータを含むかフィルタできます.
  • _source_includesは検索したいだけsourceノードのuserフィールドとmessageフィールド:
  • GET weibo/_doc/1?_source_includes=user,message
    {
      "_index" : "weibo",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 3,
      "_seq_no" : 2,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "message" : "trying out Elasticsearch",
        "user" : "niewj"
      }
    }
  • _sourceの上記の内容は、以下のように簡単に書くことができます.source
  • GET weibo/_doc/1?_source=user,message
  • source_excludesは除外したいsourceノードのmessageフィールド、残りの全表示:
  • GET weibo/_doc/1?_source_excludes=message
    {
      "_index" : "weibo",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 3,
      "_seq_no" : 2,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "post_date" : "2009-11-15T14:12:12",
        "user" : "niewj"
      }
    }

    6.直接使用ソース取得データ


    使用/{index}/source/{id}ドキュメントのみ取得ソースフィールドには、追加の内容は含まれません.例:
    GET weibo/_source/1
    {
      "user" : "niewj",
      "post_date" : "2009-11-15T14:12:12",
      "message" : "trying out Elasticsearch"
    }

    純粋なデータのみが返されることがわかります.上の_も使えますソースのフィルタフィールド:
    GET weibo/_source/1?_source=user
    {
      "user" : "niewj"
    }

    7.インデックス時のstored_fieldsパラメータ


    デフォルトでは、フィールド値は検索可能にインデックスされますが、元の値は保存されません.これは、フィールドを調べることができますが、元のフィールド値を取得することはできません.例:
    #  mapping counter ;tags ;
    PUT twitter
    {
       "mappings": {
           "properties": {
              "counter": {
                 "type": "integer",
                 "store": false
              },
              "tags": {
                 "type": "keyword",
                 "store": true
              }
           }
       }
    }

    索引レコード:
    #  , id=1
    PUT twitter/_doc/1
    {
        "counter" : 1,
        "tags" : ["red"]
    }

    クエリGET twitter/_doc/1
    {
      "_index" : "twitter",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "counter" : 1,
        "tags" : [
          "red"
        ]
      }
    }

    すべて調べることができます;stored_の使用fieldsパラメータクエリー:GET twitter/_doc/1?stored_fields=tags,counter:
    {
      "_index" : "twitter",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 1,
      "_seq_no" : 0,
      "_primary_term" : 1,
      "found" : true,
      "fields" : {
        "tags" : [
          "red"
        ]
      }
    }

    counterフィールドがなくなりました.mappingにはstore: falseが設置されているので

    8.インデックスクエリパフォーマンス関連パラメータの:refresh

    refreshは、getの前に相関スライスをリフレッシュし、検索可能にするためにtrueに設定される.これをtrueに設定する前に、システムの負荷が重すぎる(インデックス速度が低下する)可能性があるため、よく考慮して確認してください.