Elasticsearch 筆記

Elasticsearch 版本 6.2.4node

1. 當對某一type,關閉動態mapping(設爲false,非strict)時,插入新的字段是否會存儲呢?可否搜索呢?可否排序呢?bash

  建立索引:app

PUT test/
{
  "mappings": {
    "_doc": {
      "dynamic":false,
      "properties": {
        "addTime": {
          "type": "integer"
        }
      }
    }
  }
} 

查看mapping:ui

GET test/_mapping

獲得結果:spa

{
  "test": {
    "mappings": {
      "_doc": {
        "dynamic": "false",
        "properties": {
          "addTime": {
            "type": "integer"
          }
        }
      }
    }
  }
}

插入數據:blog

POST test/_doc
{
  "id":1,
  "addTime":"1536476000"
}

查看是否存入:排序

GET test/_search

獲得結果:索引

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "4zJ8vWUBHYQn1k6_tB3V",
        "_score": 1,
        "_source": {
          "id": 1,
          "addTime": "1536476000"
        }
      }
    ]
  }
}

能夠得知是會存儲的。ip

查看是否能夠搜索:it

GET test/_search
{
  "query": {
    "term": {
      "id": {
        "value": "1"
      }
    }
  }
}

獲得結果:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

能夠得知是沒法搜索的。

查看是否能夠排序:

GET test/_search
{
  "sort": [
    {
      "id": {
        "order": "desc"
      }
    }
  ]
}

獲得結果:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "No mapping found for [id] in order to sort on",
        "index_uuid": "4HMd3wH5SxWqfU-6rEfHyw",
        "index": "test"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "test",
        "node": "y_oEUnmsTqyzHKrkH54d_w",
        "reason": {
          "type": "query_shard_exception",
          "reason": "No mapping found for [id] in order to sort on",
          "index_uuid": "4HMd3wH5SxWqfU-6rEfHyw",
          "index": "test"
        }
      }
    ]
  },
  "status": 400
}

能夠得知是沒法排序的。

結論:當對某一type,關閉動態mapping(設爲false,非strict)時,插入新的字段是會被存儲的,但不能搜索和排序。

2. 對於某一字段禁止創建索引以後,搜索結果是否還存在該字段?該字段可否被搜索到?該字段可否用來排序?

   建立索引:

PUT test/
{
  "mappings": {
    "_doc": {
      "properties": {
        "addTime": {
          "type": "integer",
          "index":false
        }
      }
    }
  }
}

查看mapping:

GET test/_mapping

獲得結果:

{
  "test": {
    "mappings": {
      "_doc": {
        "properties": {
          "addTime": {
            "type": "integer",
            "index": false
          }
        }
      }
    }
  }
}

插入數據:

POST test/_doc
{
  "addTime":1536476000
}

查看是否存入:

GET test/_search

獲得結果:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "5jKPvWUBHYQn1k6_EB0i",
        "_score": 1,
        "_source": {
          "addTime": 1536476000
        }
      }
    ]
  }
}

能夠看到可以存入。

查看是否可以搜索:

GET test/_search
{
  "query": {
    "term": {
      "addTime": {
        "value": 1536476000
      }
    }
  }
}

獲得結果:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"term\" : {\n    \"addTime\" : {\n      \"value\" : 1536476000,\n      \"boost\" : 1.0\n    }\n  }\n}",
        "index_uuid": "QNQEwQONTpe5DL2KJal70g",
        "index": "test"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "test",
        "node": "y_oEUnmsTqyzHKrkH54d_w",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"term\" : {\n    \"addTime\" : {\n      \"value\" : 1536476000,\n      \"boost\" : 1.0\n    }\n  }\n}",
          "index_uuid": "QNQEwQONTpe5DL2KJal70g",
          "index": "test",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [addTime] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400
}

能夠得知不能對未索引(index:false)的字段進行搜索。

查看是否可以排序:

GET test/_search
{
  "sort": [
    {
      "addTime": {
        "order": "desc"
      }
    }
  ]
}

獲得結果:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "test",
        "_type": "_doc",
        "_id": "ryi1xmUBXnKGUBJZj3hG",
        "_score": null,
        "_source": {
          "addTime": 1536476000
        },
        "sort": [
          1536476000
        ]
      }
    ]
  }
}

得知是能夠排序的。

結論:對於某一字段禁止創建索引以後,該字段的值是能夠存入的(存在於搜索結果),可是不能搜索,能夠排序。

相關文章
相關標籤/搜索