## 復合核心字段類型
除了之前提到的簡單的標量類型,JSON還有`null`值,數組和對象,所有這些Elasticsearch都支持:
### 多值字段
我們想讓`tag`字段包含多個字段,這非常有可能發生。我們可以索引一個標簽數組來代替單一字符串:
```javascript
{ "tag": [ "search", "nosql" ]}
```
對于數組不需要特殊的映射。任何一個字段可以包含零個、一個或多個值,同樣對于全文字段將被分析并產生多個詞。
言外之意,這意味著**數組中所有值必須為同一類型**。你不能把日期和字符竄混合。如果你創建一個新字段,這個字段索引了一個數組,Elasticsearch將使用第一個值的類型來確定這個新字段的類型。
> 當你從Elasticsearch中取回一個文檔,任何一個數組的順序和你索引它們的順序一致。你取回的`_source`字段的順序同樣與索引它們的順序相同。
> 然而,數組是做為多值字段被**索引**的,它們沒有順序。在搜索階段你不能指定“第一個值”或者“最后一個值”。倒不如把數組當作一個**值集合(bag of values)**
### 空字段
當然數組可以是空的。這等價于有零個值。事實上,Lucene沒法存放`null`值,所以一個`null`值的字段被認為是空字段。
這四個字段將被識別為空字段而不被索引:
```javascript
"empty_string": "",
"null_value": null,
"empty_array": [],
"array_with_null_value": [ null ]
```
### 多層對象
我們需要討論的最后一個自然JSON數據類型是**對象(object)**——在其它語言中叫做hash、hashmap、dictionary 或者 associative array.
**內部對象(inner objects)**經常用于在另一個對象中嵌入一個實體或對象。例如,做為在`tweet`文檔中`user_name`和`user_id`的替代,我們可以這樣寫:
```javascript
{
"tweet": "Elasticsearch is very flexible",
"user": {
"id": "@johnsmith",
"gender": "male",
"age": 26,
"name": {
"full": "John Smith",
"first": "John",
"last": "Smith"
}
}
}
```
### 內部對象的映射
Elasticsearch 會動態的檢測新對象的字段,并且映射它們為 `object` 類型,將每個字段加到 `properties` 字段下
```json
{
"gb": {
"tweet": { <1>
"properties": {
"tweet": { "type": "string" },
"user": { <2>
"type": "object",
"properties": {
"id": { "type": "string" },
"gender": { "type": "string" },
"age": { "type": "long" },
"name": { <3>
"type": "object",
"properties": {
"full": { "type": "string" },
"first": { "type": "string" },
"last": { "type": "string" }
}
}
}
}
}
}
}
}
```
<1> 根對象.
<2><3> 內部對象.
對`user`和`name`字段的映射與`tweet`類型自己很相似。事實上,`type`映射只是`object`映射的一種特殊類型,我們將 `object` 稱為_根對象_。它與其他對象一模一樣,除非它有一些特殊的頂層字段,比如 `_source`, `_all` 等等。
### 內部對象是怎樣被索引的
Lucene 并不了解內部對象。 一個 Lucene 文件包含一個鍵-值對應的扁平表單。 為了讓 Elasticsearch 可以有效的索引內部對象,將文件轉換為以下格式:
```javascript
{
"tweet": [elasticsearch, flexible, very],
"user.id": [@johnsmith],
"user.gender": [male],
"user.age": [26],
"user.name.full": [john, smith],
"user.name.first": [john],
"user.name.last": [smith]
}
```
_內部欄位_可被歸類至name,例如`"first"`。 為了區別兩個擁有相同名字的欄位,我們可以使用完整_路徑_,例如`"user.name.first"` 或甚至`類型`名稱加上路徑:`"tweet.user.name.first"`。
> 注意: 在以上扁平化文件中,并沒有欄位叫作`user`也沒有欄位叫作`user.name`。 Lucene 只索引階層或簡單的值,而不會索引復雜的資料結構。
## 對象-數組
### 內部對象數組
最后,一個包含內部對象的數組如何索引。 我們有個數組如下所示:
```json
{
"followers": [
{ "age": 35, "name": "Mary White"},
{ "age": 26, "name": "Alex Jones"},
{ "age": 19, "name": "Lisa Smith"}
]
}
```
此文件會如我們以上所說的被扁平化,但其結果會像如此:
```json
{
"followers.age": [19, 26, 35],
"followers.name": [alex, jones, lisa, smith, mary, white]
}
```
`{age: 35}`與`{name: Mary White}`之間的關聯會消失,因每個多值的欄位會變成一個值集合,而非有序的陣列。 這讓我們可以知道:
* _是否有26歲的追隨者?_
但我們無法取得準確的資料如:
* _是否有26歲的追隨者**且名字叫Alex Jones?**_
關聯內部對象可解決此類問題,我們稱之為_嵌套_對象,我們之後會在嵌套對象中提到它。
- Introduction
- 入門
- 是什么
- 安裝
- API
- 文檔
- 索引
- 搜索
- 聚合
- 小結
- 分布式
- 結語
- 分布式集群
- 空集群
- 集群健康
- 添加索引
- 故障轉移
- 橫向擴展
- 更多擴展
- 應對故障
- 數據
- 文檔
- 索引
- 獲取
- 存在
- 更新
- 創建
- 刪除
- 版本控制
- 局部更新
- Mget
- 批量
- 結語
- 分布式增刪改查
- 路由
- 分片交互
- 新建、索引和刪除
- 檢索
- 局部更新
- 批量請求
- 批量格式
- 搜索
- 空搜索
- 多索引和多類型
- 分頁
- 查詢字符串
- 映射和分析
- 數據類型差異
- 確切值對決全文
- 倒排索引
- 分析
- 映射
- 復合類型
- 結構化查詢
- 請求體查詢
- 結構化查詢
- 查詢與過濾
- 重要的查詢子句
- 過濾查詢
- 驗證查詢
- 結語
- 排序
- 排序
- 字符串排序
- 相關性
- 字段數據
- 分布式搜索
- 查詢階段
- 取回階段
- 搜索選項
- 掃描和滾屏
- 索引管理
- 創建刪除
- 設置
- 配置分析器
- 自定義分析器
- 映射
- 根對象
- 元數據中的source字段
- 元數據中的all字段
- 元數據中的ID字段
- 動態映射
- 自定義動態映射
- 默認映射
- 重建索引
- 別名
- 深入分片
- 使文本可以被搜索
- 動態索引
- 近實時搜索
- 持久化變更
- 合并段
- 結構化搜索
- 查詢準確值
- 組合過濾
- 查詢多個準確值
- 包含,而不是相等
- 范圍
- 處理 Null 值
- 緩存
- 過濾順序
- 全文搜索
- 匹配查詢
- 多詞查詢
- 組合查詢
- 布爾匹配
- 增加子句
- 控制分析
- 關聯失效
- 多字段搜索
- 多重查詢字符串
- 單一查詢字符串
- 最佳字段
- 最佳字段查詢調優
- 多重匹配查詢
- 最多字段查詢
- 跨字段對象查詢
- 以字段為中心查詢
- 全字段查詢
- 跨字段查詢
- 精確查詢
- 模糊匹配
- Phrase matching
- Slop
- Multi value fields
- Scoring
- Relevance
- Performance
- Shingles
- Partial_Matching
- Postcodes
- Prefix query
- Wildcard Regexp
- Match phrase prefix
- Index time
- Ngram intro
- Search as you type
- Compound words
- Relevance
- Scoring theory
- Practical scoring
- Query time boosting
- Query scoring
- Not quite not
- Ignoring TFIDF
- Function score query
- Popularity
- Boosting filtered subsets
- Random scoring
- Decay functions
- Pluggable similarities
- Conclusion
- Language intro
- Intro
- Using
- Configuring
- Language pitfalls
- One language per doc
- One language per field
- Mixed language fields
- Conclusion
- Identifying words
- Intro
- Standard analyzer
- Standard tokenizer
- ICU plugin
- ICU tokenizer
- Tidying text
- Token normalization
- Intro
- Lowercasing
- Removing diacritics
- Unicode world
- Case folding
- Character folding
- Sorting and collations
- Stemming
- Intro
- Algorithmic stemmers
- Dictionary stemmers
- Hunspell stemmer
- Choosing a stemmer
- Controlling stemming
- Stemming in situ
- Stopwords
- Intro
- Using stopwords
- Stopwords and performance
- Divide and conquer
- Phrase queries
- Common grams
- Relevance
- Synonyms
- Intro
- Using synonyms
- Synonym formats
- Expand contract
- Analysis chain
- Multi word synonyms
- Symbol synonyms
- Fuzzy matching
- Intro
- Fuzziness
- Fuzzy query
- Fuzzy match query
- Scoring fuzziness
- Phonetic matching
- Aggregations
- overview
- circuit breaker fd settings
- filtering
- facets
- docvalues
- eager
- breadth vs depth
- Conclusion
- concepts buckets
- basic example
- add metric
- nested bucket
- extra metrics
- bucket metric list
- histogram
- date histogram
- scope
- filtering
- sorting ordering
- approx intro
- cardinality
- percentiles
- sigterms intro
- sigterms
- fielddata
- analyzed vs not
- 地理坐標點
- 地理坐標點
- 通過地理坐標點過濾
- 地理坐標盒模型過濾器
- 地理距離過濾器
- 緩存地理位置過濾器
- 減少內存占用
- 按距離排序
- Geohashe
- Geohashe
- Geohashe映射
- Geohash單元過濾器
- 地理位置聚合
- 地理位置聚合
- 按距離聚合
- Geohash單元聚合器
- 范圍(邊界)聚合器
- 地理形狀
- 地理形狀
- 映射地理形狀
- 索引地理形狀
- 查詢地理形狀
- 在查詢中使用已索引的形狀
- 地理形狀的過濾與緩存
- 關系
- 關系
- 應用級別的Join操作
- 扁平化你的數據
- Top hits
- Concurrency
- Concurrency solutions
- 嵌套
- 嵌套對象
- 嵌套映射
- 嵌套查詢
- 嵌套排序
- 嵌套集合
- Parent Child
- Parent child
- Indexing parent child
- Has child
- Has parent
- Children agg
- Grandparents
- Practical considerations
- Scaling
- Shard
- Overallocation
- Kagillion shards
- Capacity planning
- Replica shards
- Multiple indices
- Index per timeframe
- Index templates
- Retiring data
- Index per user
- Shared index
- Faking it
- One big user
- Scale is not infinite
- Cluster Admin
- Marvel
- Health
- Node stats
- Other stats
- Deployment
- hardware
- other
- config
- dont touch
- heap
- file descriptors
- conclusion
- cluster settings
- Post Deployment
- dynamic settings
- logging
- indexing perf
- rolling restart
- backup
- restore
- conclusion