properties (属性)

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/properties.html(修改该链接为官网对应的链接)

译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=10027013(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : 郭峰ApacheCNApache中文网

properties (属性)

type(类型)映射、object fields(对象字段)和 nested fields(嵌入字段)包含的 sub-fields(子字段),称之为 properties(属性)。这些 properties(属性)可以为任意 datatype(数据类型),包括 object(对象)和 nested(嵌入数据)。properties(属性)可以通过以下方式加入:

  • 通过创建索引是明确地定义他们。
  • 通过使用 PUT mapping** API** 添加或更新映射类型时明确地定义他们。
  • 索引包含新字段的文档时动态的加入。

下面举例演示如何将 properties(属性)加入 mapping type(映射类型)、object field(对象字段)和 nested field(嵌入字段)中:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "my_type": { #1
  5. "properties": {
  6. "manager": { #2
  7. "properties": {
  8. "age": { "type": "integer" },
  9. "name": { "type": "text" }
  10. }
  11. },
  12. "employees": { #3
  13. "type": "nested",
  14. "properties": {
  15. "age": { "type": "integer" },
  16. "name": { "type": "text" }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. '
  24. curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'#4
  25. {
  26. "region": "US",
  27. "manager": {
  28. "name": "Alice White",
  29. "age": 30
  30. },
  31. "employees": [
  32. {
  33. "name": "John Smith",
  34. "age": 34
  35. },
  36. {
  37. "name": "Peter Brown",
  38. "age": 26
  39. }
  40. ]
  41. }
  42. '

| 1 | my_type 映射类型下的 properties(属性) | | 2 | manager 对象字段下的 properties(属性) | | 3 | employee 嵌入字段下的 properties(属性) | | 4 | 对应上述映射的一个示例文档 |

注意

同一索引下的相同名字的字段可以设置不同的 properties(属性)。新的 properties(属性)可以通过 PUT mapping API 加入已经存在的字段。

dot notation (“.”符号)

通过使用“.”符号可以使内嵌字段引入查询、聚合等功能中:

  1. curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "query": {
  4. "match": {
  5. "manager.name": "Alice White" #1
  6. }
  7. },
  8. "aggs": {
  9. "Employees": {
  10. "nested": {
  11. "path": "employees"
  12. },
  13. "aggs": {
  14. "Employee Ages": {
  15. "histogram": {
  16. "field": "employees.age", #2
  17. "interval": 5
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. '

重点

内嵌字段必须使用完整的路径