[TOC]
## 簡介
- Elasticsearch 搜尋主要分為2種方式
1. Queries
2. Aggregations
## [資源連結](https://github.com/LisaHJung/Part-2-Understanding-the-relevance-of-your-search-with-Elasticsearch-and-Kibana-)
[本次練習資料下載](https://www.kaggle.com/datasets/rmisra/news-category-dataset)
## Queries
查詢符合條件的文檔
### 取得 Index 相關的文檔
語法:
```
GET 要查詢的Index/_search
```
Example:
```
GET news_headlines/_search
```
Response:
顯示符合條件的資料 & 顯示10筆相關資訊
==Elasticsearch 考慮效能,預設最多取得 10,000 筆資料==

### 取的 Index 相關的文檔實際數量
語法:
```
GET 要查詢的Index/_search
{
"track_total_hits": true
}
```
Example:
```
GET news_headlines/_search
{
"track_total_hits": true
}
```
Response:
==可以看到這邊的hits數量為 209,527==

### 查詢特定條件內的資料
語法:
```
GET 要查詢的Index/_search
{
"query": {
"在此指定查詢類型": {
"欄位名稱": {
"gte": "預計查詢欄位資料的最小值",
"lte": "預計查詢欄位資料的最大值"
}
}
}
}
```
> 查詢特定時間內的資料
Example:
```
GET news_headlines/_search
{
"query": {
"range": {
"date": {
"gte": "2015-06-20",
"lte": "2015-09-22"
}
}
}
}
```
Response:

## Aggregations
語法:
```
GET 要查詢的Index/_search
{
"aggs": {
"命名聚合的名稱": {
"指定聚合類型": {
"field": "要聚合欄位",
"size": 要返回buckets的數量
}
}
}
}
```
### 分析顯示資料內 news headlines 的種類
Example:
```
GET news_headlines/_search
{
"aggs": {
"by_category": {
"terms": {
"field": "category",
"size": 100
}
}
}
}
```
Response:

## 結合 query & aggreation 查詢
### 搜索類別中最重要的術語
語法:
```
GET 要查詢的Index/_search
{
"query": {
"match": {
"欄位名稱": "指定查找的的資料"
}
},
"aggregations": {
"為此聚合查詢命名": {
"significant_text": {
"field": "查詢的欄位"
}
}
}
}
```
Example: (查詢ENTERTAINMENT類別 & 分析 headline 出現的字段)
```
GET news_headlines/_search
{
"query": {
"match": {
"category": "ENTERTAINMENT" // 查詢ENTERTAINMENT類別的資料
}
},
"aggregations": {
"popular_in_entertainment": { // 分析 ENTERTAINMENT 較常出現的字段
"significant_text": {
"field": "headline" 分析欄位 headline
}
}
}
}
```
Response:
==doc_count== - 符合文檔
==bg_count== - 背景資料量

## Precision and Recall
Precision"(精確率)和 "Recall"(召回率)。這兩個指標是信息檢索領域中常用的評估指標,用於評估搜索系統的性能。
==Precision & Recall 判斷哪些是需要返回的資料,但不判斷資料相關性的高低==
==判斷資料相關性高低的依據是 Ranking (score的高低)==
1. 精確率(Precision):
精確率是指在檢索結果中,真正與查詢相關的文檔數量佔總返回文檔數量的比例。簡而言之,它評 了搜索結果的準確性。精確率的公式如下:
Precision = (查詢相關且被檢索到的文檔數量) / (總被檢索到的文檔數量)
以一個簡單的例子來說,如果一個查詢返回了10個文檔,而其中有6個是與查詢相關的,那麼精確率 就是6/10,即0.6。
2. 召回率(Recall):
召回率是指在所有相關的文檔中,真正被檢索到的文檔數量佔總相關文檔數量的比例。它評估了搜索系統是否能夠找到所有相關的文檔,而不漏掉任何一個。召回率的公式如下:
Recall = (查詢相關且被檢索到的文檔數量) / (總相關文檔數量)
例如,如果有100個與查詢相關的文檔,而搜索操作只返回了其中的80個,則召回率為80/100,即0.8。
> 精確率和召回率是一對相互衝突的指標。提高精確率可能會降低召回率,反之亦然。搜索引擎的目標通常是在兩者之間找到一個平衡,以提供既準確又全面的搜索結果。這可以通過調整搜索引擎的配置、優化查詢、提高文檔的相關性得分等方式來實現
### 提升查詢Recall
語法:
```
GET 要查詢的Index/_search
{
"query": {
"match": {
"想要查詢的欄位": {
"query": "搜索的關鍵字"
}
}
}
}
```
Example:
```
GET news_headlines/_search
{
"query": {
"match": {
"headline": {
"query": "Khloe Kardashian Kendall Jenner"
}
}
}
}
```
搜索預設使用 "or" 邏輯,只要查詢內容有任何搜索的關鍵字,Elasticsearch 就會將該文檔視為命中。
"or" 會提高文檔被命中的機率,因此提高了Recall,然而,命中文檔與搜索的關鍵字關西鬆散,因此降低了精度。
Response:

### 提升查詢Precision
可藉由加入 "and" 操作符提高查詢精度。
語法:
```
GET enter_name_of_index_here/_search
{
"query": {
"match": {
"Specify the field you want to search": {
"query": "Enter search terms",
"operator": "and"
}
}
}
}
```
Example:
```
GET news_headlines/_search
{
"query": {
"match": {
"headline": {
"query": "Khloe Kardashian Kendall Jenner",
"operator": "and"
}
}
}
}
```
"and" 會提高搜索到的文檔內容更加精確,因此提高了precision,但是,它會減少返回的命中數,從而導致召回率降低。
Response:

#### `minimum_should_match`
此參數允許您指定文檔應包含在搜索關鍵字中的最少關聯數。 此參數使您可以更好地控制 precision 和 recall。
語法:
```
GET enter_name_of_index_here/_search
{
"query": {
"match": {
"headline": {
"query": "Enter search term here",
"minimum_should_match": Enter a number here
}
}
}
}
```
Example:
```
GET news_headlines/_search
{
"query": {
"match": {
"headline": {
"query": "Khloe Kardashian Kendall Jenner",
"minimum_should_match": 3
}
}
}
}
```
最少要符合==4個關鍵字中的3個==
Response:
