## 相關性簡介
我們曾經講過,默認情況下,返回結果是按相關性倒序排列的。
但是什么是相關性? 相關性如何計算?
每個文檔都有相關性評分,用一個相對的浮點數字段 `_score` 來表示 -- `_score` 的評分越高,相關性越高。
查詢語句會為每個文檔添加一個 `_score` 字段。評分的計算方式取決于不同的查詢類型 --
不同的查詢語句用于不同的目的:`fuzzy` 查詢會計算與關鍵詞的拼寫相似程度,`terms`查詢會計算
找到的內容與關鍵詞組成部分匹配的百分比,但是一般意義上我們說的全文本搜索是指計算內容與關鍵詞的類似程度。
ElasticSearch的相似度算法被定義為 TF/IDF,即檢索詞頻率/反向文檔頻率,包括一下內容:
檢索詞頻率::
檢索詞在該字段出現的頻率?出現頻率越高,相關性也越高。
字段中出現過5次要比只出現過1次的相關性高。
反向文檔頻率::
每個檢索詞在索引中出現的頻率?頻率越高,相關性越低。
檢索詞出現在多數文檔中會比出現在少數文檔中的權重更低,
即檢驗一個檢索詞在文檔中的普遍重要性。
字段長度準則::
字段的長度是多少?長度越長,相關性越低。
檢索詞出現在一個短的 `title` 要比同樣的詞出現在一個長的 `content` 字段。
單個查詢可以使用TF/IDF評分標準或其他方式,比如短語查詢中檢索詞的距離或模糊查詢里的檢索詞相似度。
相關性并不只是全文本檢索的專利。也適用于`yes|no`的子句,匹配的子句越多,相關性評分越高。
如果多條查詢子句被合并為一條復合查詢語句,比如 `bool` 查詢,則每個查詢子句計算得出的評分會被合并到總的相關性評分中。
## 理解評分標準
當調試一條復雜的查詢語句時,想要理解相關性評分 `_score` 是比較困難的。ElasticSearch 在
每個查詢語句中都有一個explain參數,將 `explain` 設為 `true` 就可以得到更詳細的信息。
```Javascript
GET /_search?explain <1>
{
"query" : { "match" : { "tweet" : "honeymoon" }}
}
```
<1> `explain` 參數可以讓返回結果添加一個 `_score` 評分的得來依據。
****
增加一個 `explain` 參數會為每個匹配到的文檔產生一大堆額外內容,但是花時間去理解它是很有意義的。
如果現在看不明白也沒關系 -- 等你需要的時候再來回顧這一節就行。下面我們來一點點的了解這塊知識點。
****
首先,我們看一下普通查詢返回的元數據:
```Javascript
{
"_index" : "us",
"_type" : "tweet",
"_id" : "12",
"_score" : 0.076713204,
"_source" : { ... trimmed ... },
}
```
這里加入了該文檔來自于哪個節點哪個分片上的信息,這對我們是比較有幫助的,因為詞頻率和
文檔頻率是在每個分片中計算出來的,而不是每個索引中:
```Javascript
"_shard" : 1,
"_node" : "mzIVYCsqSWCG_M_ZffSs9Q",
```
然后返回值中的 `_explanation` 會包含在每一個入口,告訴你采用了哪種計算方式,并讓你知道計算的結果以及其他詳情:
```Javascript
"_explanation": { <1>
"description": "weight(tweet:honeymoon in 0)
[PerFieldSimilarity], result of:",
"value": 0.076713204,
"details": [
{
"description": "fieldWeight in 0, product of:",
"value": 0.076713204,
"details": [
{ <2>
"description": "tf(freq=1.0), with freq of:",
"value": 1,
"details": [
{
"description": "termFreq=1.0",
"value": 1
}
]
},
{ <3>
"description": "idf(docFreq=1, maxDocs=1)",
"value": 0.30685282
},
{ <4>
"description": "fieldNorm(doc=0)",
"value": 0.25,
}
]
}
]
}
```
<1> `honeymoon` 相關性評分計算的總結
<2> 檢索詞頻率
<3> 反向文檔頻率
<4> 字段長度準則
>**重要**:
> 輸出 `explain` 結果代價是十分昂貴的,它只能用作調試工具
> --千萬不要用于生產環境。
第一部分是關于計算的總結。告訴了我們 `"honeymoon"` 在 `tweet`字段中的檢索詞頻率/反向文檔頻率或 TF/IDF,
(這里的文檔 `0` 是一個內部的ID,跟我們沒有關系,可以忽略。)
然后解釋了計算的權重是如何計算出來的:
檢索詞頻率:
檢索詞 `honeymoon` 在 `tweet` 字段中的出現次數。
反向文檔頻率:
檢索詞 `honeymoon` 在 `tweet` 字段在當前文檔出現次數與索引中其他文檔的出現總數的比率。
字段長度準則:
文檔中 `tweet` 字段內容的長度 -- 內容越長,值越小。
復雜的查詢語句解釋也非常復雜,但是包含的內容與上面例子大致相同。
通過這段描述我們可以了解搜索結果是如何產生的。
>**提示**:
>JSON形式的explain描述是難以閱讀的
>但是轉成 YAML 會好很多,只需要在參數中加上 `format=yaml`
## Explain Api
#### 文檔是如何被匹配到的
當`explain`選項加到某一文檔上時,它會告訴你為何這個文檔會被匹配,以及一個文檔為何沒有被匹配。
請求路徑為 `/index/type/id/_explain`, 如下所示:
```Javascript
GET /us/tweet/12/_explain
{
"query" : {
"filtered" : {
"filter" : { "term" : { "user_id" : 2 }},
"query" : { "match" : { "tweet" : "honeymoon" }}
}
}
}
```
除了上面我們看到的完整描述外,我們還可以看到這樣的描述:
```Javascript
"failure to match filter: cache(user_id:[2 TO 2])"
```
也就是說我們的 `user_id` 過濾子句使該文檔不能匹配到。
- 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