zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Elasticsearch 映射2

2023-03-20 14:52:20 时间

查看字段的mapping

可以限定只查看指定的字段类型,而不是所有字段

[root@h102 ~]# curl 'localhost:9200/abc/_mapping/test/field/age?pretty'
{
  "abc" : {
    "mappings" : {
      "test" : {
        "age" : {
          "full_name" : "age",
          "mapping" : {
            "age" : {
              "type" : "long"
            }
          }
        }
      }
    }
  }
}
[root@h102 ~]#

查看API为

host:port/{index}/{type}/_mapping/field/{field}

{index}{type}{field} 中可以使用逗号作为分割来指定一个名称列表,以同时指定多个想查看的对象 . 如果要代表所有的索引 可以在 {index} 中使用 _all

补充特性

匹配符

可以使用逗号作为分割来指定一个名称列表,同时也可以使用匹配符

[root@h102 ~]# curl 'localhost:9200/abc/_mapping/t*/field/a*?pretty'
{
  "abc" : {
    "mappings" : {
      "test" : {
        "age" : {
          "full_name" : "age",
          "mapping" : {
            "age" : {
              "type" : "long"
            }
          }
        }
      }
    }
  }
}
[root@h102 ~]#

嵌套文档

这里的示例比较简单,如果是嵌套文档怎么办呢,在json中,这种情况可不少见

这时用 . 来进行指定

{
     "article": {
         "properties": {
             "id": { "type": "string" },
             "title":  { "type": "string"},
             "abstract": { "type": "string"},
             "author": {
                 "properties": {
                     "id": { "type": "string" },
                     "name": { "type": "string" }
                 }
             }
         }
     }
}

author.id 指代 author 中的 id

author.name 指代 author 中的 name

Tip: ES是使用Lucene 实现索引的,而Lucene并不懂多层对象,Lucene只是将它们看作一个个的扁平的 Key-Value 对, 为了让它可以处理多层对象,ES将嵌套的多层结构映射成了点分多层结构,user中的id和name 分别被当成 user.id 和 user.name 来处理

Note: 在ES中列表是没有顺序的,类似于集合的概念

显示所有默认属性

加上 include_defaults=true 就可以将隐藏的默认属性都显示出来

[root@h102 ~]# curl 'localhost:9200/abc/_mapping/test/field/age?pretty&include_defaults=true'
{
  "abc" : {
    "mappings" : {
      "test" : {
        "age" : {
          "full_name" : "age",
          "mapping" : {
            "age" : {
              "type" : "long",
              "boost" : 1.0,
              "index" : "not_analyzed",
              "store" : false,
              "doc_values" : true,
              "term_vector" : "no",
              "norms" : {
                "enabled" : false
              },
              "index_options" : "docs",
              "analyzer" : "_long/16",
              "search_analyzer" : "_long/max",
              "similarity" : "default",
              "fielddata" : { },
              "ignore_malformed" : false,
              "coerce" : true,
              "precision_step" : 16,
              "null_value" : null,
              "include_in_all" : false
            }
          }
        }
      }
    }
  }
}
[root@h102 ~]#