<!--秀川譯-->
### 關聯失效
在我們去討論多字段檢索中的更復雜的查詢前,讓我們順便先解釋一下為什么我們只用一個主分片來創建索引。
有時有的新手會開一個問題說通過相關性排序沒有效果,并且提供了一小段復制的結果:該用戶創建了一些文檔,執行了一個簡單的查詢,結果發現相關性較低的結果排在了相關性較高的結果的前面。
為了理解為什么會出現這樣的結果,我們假設用兩個分片創建一個索引,以及索引10個文檔,6個文檔包含詞 `foo`,這樣可能會出現分片1中有3個文檔包含 `foo`,分片2中也有三個文檔包含 `foo`。換句話說,我們的文檔做了比較好的分布式。
在相關性介紹這一節,我們描述了Elasticsearch默認的相似算法,叫做詞頻率/反轉文檔頻率(TF/IDF)。詞頻率是一個詞在我們當前查詢的文檔的字段中出現的次數。出現的次數越多,相關性就越大。反轉文檔頻率指的是該索引中所有文檔數與出現這個詞的文件數的百分比,詞出現的頻率越大,IDF越小。
然而,由于性能問題,Elasticsearch不通過索引中所有的文檔計算IDF。每個分片會為分片中所有的文檔計算一個本地的IDF取而代之。
因為我們的文檔做了很好的分布式,每個分片的IDF是相同的。現在假設5個包含`foo`的文檔在分片1中,以及其他6各文檔在分片2中。在這個場景下,詞`foo`在第一個分片中是非常普通的(因此重要性較小),但是在另一個分片中是很稀少的(因此重要性較高)。這些區別在IDF中就會產生不正確的結果。
事實證明,這并不是一個問題。你索引越多的文檔,本地IDF和全局IDF的區別就會越少。在實際工作的數據量下,本地IDF立刻能夠很好的工作(With real-world
volumes of data, the local IDFs soon even out,不知道這么翻譯合不合適)。所以問題不是因為關聯失效,而是因為數據太少。
為了測試的目的,對于這個問題,有兩種方法可以奏效。第一種方法是創建一個只有一個主分片的索引,像我們介紹`match`查詢那節一樣做。如果你只有一個分片,那么本地IDF就是全局IDF。
第二種方法是在你們請求中添加`?search_type=dfs_query_then_fetch`。`dfs`就是*Distributed Frequency Search*,并且它會告訴Elasticsearch檢查每一個分片的本地IDF為了計算整個索引的全局IDF。
> 提示:不要把`dfs_query_then_fetch`用于生產環境。它實在是沒有必要。只要有足夠的數據就能夠確保詞頻率很好的分布。沒有理由在每個你要執行的查詢中添加額外的DFS步驟。
<!--
[[relevance-is-broken]]
=== Relevance Is Broken!
Before we move on to discussing more-complex queries in
<<multi-field-search>>, let's make a quick detour to explain why we
<<match-test-data,created our test index>> with just one primary shard.
Every now and again a new user opens an issue claiming that sorting by
relevance((("relevance", "differences in IDF producing incorrect results"))) is broken and offering a short reproduction: the user indexes a few
documents, runs a simple query, and finds apparently less-relevant results
appearing above more-relevant results.
To understand why this happens, let's imagine that we create an index with two
primary shards and we index ten documents, six of which contain the word `foo`.
It may happen that shard 1 contains three of the `foo` documents and shard
2 contains the other three. In other words, our documents are well distributed.
In <<relevance-intro>>, we described the default similarity algorithm used in
Elasticsearch, ((("Term Frequency/Inverse Document Frequency (TF/IDF) similarity algorithm")))called _term frequency / inverse document frequency_ or TF/IDF.
Term frequency counts the number of times a term appears within the field we are
querying in the current document. The more times it appears, the more
relevant is this document. The _inverse document frequency_ takes((("inverse document frequency")))((("IDF", see="inverse document frequency"))) into account
how often a term appears as a percentage of _all the documents in the index_.
The more frequently the term appears, the less weight it has.
However, for performance reasons, Elasticsearch doesn't calculate the IDF
across all documents in the index.((("shards", "local inverse document frequency (IDF)"))) Instead, each shard calculates a local IDF
for the documents contained _in that shard_.
Because our documents are well distributed, the IDF for both shards will be
the same. Now imagine instead that five of the `foo` documents are on shard 1,
and the sixth document is on shard 2. In this scenario, the term `foo` is
very common on one shard (and so of little importance), but rare on the other
shard (and so much more important). These differences in IDF can produce
incorrect results.
In practice, this is not a problem. The differences between local and global
IDF diminish the more documents that you add to the index. With real-world
volumes of data, the local IDFs soon even out. The problem is not that
relevance is broken but that there is too little data.
For testing purposes, there are two ways we can work around this issue. The
first is to create an index with one primary shard, as we did in the section
introducing the <<match-query,`match` query>>. If you have only one shard, then
the local IDF _is_ the global IDF.
The second workaround is to add `?search_type=dfs_query_then_fetch` to your
search requests. The `dfs` stands((("search_type", "dfs_query_then_fetch")))((("dfs_query_then_fetch search type")))((("DFS (Distributed Frequency Search)"))) for _Distributed Frequency Search_, and it
tells Elasticsearch to first retrieve the local IDF from each shard in order
to calculate the global IDF across the whole index.
TIP: Don't use `dfs_query_then_fetch` in production. It really isn't
required. Just having enough data will ensure that your term frequencies are
well distributed. There is no reason to add this extra DFS step to every query
that you run.
-->
- 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