es - elasticsearch mapping - parameters - index

405 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :index有什么特点?
答 :
在这里插入图片描述
问 :index如何使用?
答 :

# index
PUT /index_test
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type"   : "text",
        "index"  : false,
        "fields" : {
          "keyword" : {
            "type"  : "keyword",
            "index" : true
          }
        }
      }
    }
  }
}

# 索引
POST /index_test/_doc/1
{
  "name" : "hello"
}

# 搜索,name没有索引,所以无法搜索
GET /index_test/_search
{
  "query" : {
    "match" : {
      "name" : "hello"
    }
  }
}

# 结果
{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Cannot search on field [name] since it is not indexed.",
        "index_uuid" : "Vp0JdQBLSP69AC8ZWiAKLQ",
        "index" : "index_test"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "index_test",
        "node" : "VK0GF1KyQGKmM_0KvgHHnw",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Cannot search on field [name] since it is not indexed.",
          "index_uuid" : "Vp0JdQBLSP69AC8ZWiAKLQ",
          "index" : "index_test",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Cannot search on field [name] since it is not indexed."
          }
        }
      }
    ]
  },
  "status" : 400
}


# 搜索,name.keyword索引了,所以可以搜索
GET /index_test/_search
{
  "query" : {
    "match" : {
      "name.keyword" : "hello"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "index_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "name" : "hello"
        }
      }
    ]
  }
}