自学内容网 自学内容网

ES——域的属性

指的是索引中文档属性或字段
在这里插入图片描述


index

描述:指定该域是否创建索引。只有设置为true时,才能根据该域的关键词查询文档。如果设置为false,则该域不会被索引,因此无法通过该域进行搜索。
示例

PUT /student1
{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer",
        "index": true
      }
    }
  }
}

PUT /student2
{
  "mappings": {
    "properties": {
      "id": {
        "type": "integer",
        "index": false
      }
    }
  }
}

POST /student1/_doc/1
{
  "id":1
}
POST /student2/_doc/1
{
  "id":1
}

GET /student1/_search
{
  "query": {
    "term": {
      "id": "1"
    }
  }
}

GET /student2/_search
{
  "query": {
    "term": {
      "id": "1"
    }
  }
}

student1结果输出
正常查询

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student1",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1
        }
      }
    ]
  }
}

student2结果输出
400报错,没建立倒排索引

{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Cannot search on field [id] since it is not indexed.",
        "index_uuid" : "lazrSPmCSAuyr-Bw4phQ3g",
        "index" : "student2"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "student2",
        "node" : "XwyTfyX-RQCee8Bm1dxbdQ",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Cannot search on field [id] since it is not indexed.",
          "index_uuid" : "lazrSPmCSAuyr-Bw4phQ3g",
          "index" : "student2",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Cannot search on field [id] since it is not indexed."
          }
        }
      }
    ]
  },
  "status" : 400
}

type

描述:定义域的数据类型,决定了Elasticsearch如何存储和索引数据。

核心类型具体类型
字符串类型text
整数类型long, integer, short, byte
浮点类型double, float
日期类型date
布尔类型boolean
数组类型array (of any other datatype)
对象类型object (typically a JSON object)
不分词的字符串keyword

keyword示例
查询时必须输入一样的内容才可以正确查询,否则查不到(比如姓名,手机号等等不可分词的内容)

PUT /student2
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "index": true
      }
    }
  }
}
POST /student2/_doc/1
{
  "name":"I am wunaiieq"
}
GET /student2/_search
{
  "query": {
    "term": {
      "name": "I am wunaiieq"
    }
  }
}

store

描述:控制是否单独存储域的值。如果设置为true,则可以单独查询该域的值,而不需要通过_source字段获取整个文档。(一般使用在经常查询的列中)
简言:直接查询这个字段下所有的值
示例

PUT /student1
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "index": true,
        "store": true
      },
      "age":{
        "type": "integer"
      }
    }
  }
}
POST /student1/_doc/1
{
  "name":"I am wunaiieq01",
  "age":101
}
GET /student1/_search
{
  "stored_fields": ["name"]
}


原文地址:https://blog.csdn.net/wusuoweiieq/article/details/144372447

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!