1. 程式人生 > >Elasticsearch 筆記

Elasticsearch 筆記

div its ase fail 插入數據 搜索結果 ucc eas score

Elasticsearch 版本 6.2.4

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

創建索引:

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

插入數據:

POST test_xzy/_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"
        }
      }
    ]
  }
}

  

可以得知是會存儲的。

查看是否可以搜索:

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

  

得到結果:

{
  "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"
        }
      }
    ]
  }
}

  

可以得知是可以搜索的。

查看是否可以排序:

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

  

得到結果:

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

  

可以得知是可以排序的。

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

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

創建索引:

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

  

插入數據:

PUT test_xzy/
{
  "mappings": {
    "_doc": {
      "dynamic":false,
      "properties": {
        "addTime": {
          "type": "integer"
        },
        "id":{
          "type":"integer",
          "index":false
        }
      }
    }
  }
}

  

查看是否存入:

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": {
          "id": 1,
          "addTime": "1536476000"
        }
      }
    ]
  }
}

  

可以看到能夠存入。

查看是否能夠搜索:

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

  

得到結果:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"term\" : {\n    \"id\" : {\n      \"value\" : \"1\",\n      \"boost\" : 1.0\n    }\n  }\n}",
        "index_uuid": "cNTINYvNTo6Gxbcq8gnJgA",
        "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    \"id\" : {\n      \"value\" : \"1\",\n      \"boost\" : 1.0\n    }\n  }\n}",
          "index_uuid": "cNTINYvNTo6Gxbcq8gnJgA",
          "index": "test",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [id] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400
}

  

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

查看是否能夠排序:

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

  

得到結果:

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

  

得知是可以排序的。

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

  

 

Elasticsearch 筆記