# 搜索您的数据
## date 2021-06-19
## 大纲
- [Collapse search results](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/collapse-search-results.html)
- [Filter search results](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/filter-search-results.html)
- [Highlighting](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/highlighting.html)
- [Long-running searches](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/async-search-intro.html)
- [Near real-time search](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/near-real-time.html)
- [Paginate search results](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/paginate-search-results.html)
- [Retrieve inner hits](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/inner-hits.html)
- [Retrieve selected fields](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/search-fields.html)
- [Search across clusters](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/modules-cross-cluster-search.html)
- [Search multiple data streams and indices](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/search-multiple-indices.html)
- [Search shard routing](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/search-shard-routing.html)
- [Sort search results](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/sort-search-results.html)
## 英语
## 笔记
[Search across clusters](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/modules-cross-cluster-search.html)
```js
// 集群搜索,首先按照如下步骤
// 1. 设置跨集群相关配置
// 2. 执行搜索
// 3. 注意事项
```
[Search multiple data streams and indices](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/search-multiple-indices.html)
```js
// 1. Index boost 不同权重,表明不同index 的重要性
GET /_search
{
"indices_boost": [
{ "my-index-000001": 1.4 },
{ "my-index-000002": 1.3 }
]
}
```
[Search shard routing](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/search-shard-routing.html) (路由)
```js
// 1. Set a preference
GET /my-index-000001/_search?preference=_local
preference=_local : search to shards on the local node.
preference=my-test-xx : 多次查询, 尽可能的落到同一个节点, 充分利用内存
// 2. Use a routing value
写入数据的时候,指定 routing
查询数据的时候,也指定 rouging , 减少过多分片的查询, 极大的提高查询性能
// 3. Search concurrency and parallelism (用处不大)
GET /my-index-000001/_search?max_concurrent_shard_requests=3
控制并行度, 减少故障, 不理解这个参数, 感觉用处不大
```
[Sort search results](https://www.elastic.co/guide/en/elasticsearch/reference/7.12/sort-search-results.html) (排序)
```json
// 1. 默认 & 默认排序字段
_score : 分数排序
_doc : index order 排序, 这个字段主要被设计来排序, 排序性能很高
默认行为: _score 默认降序, 其他字段默认生效
// 2. 基本语法如下
"sort" : [
{ "post_date" : {"order" : "asc"}},
{ "age" : "desc" },
"_score"
]
// 3. 多值字段排序
Sort mode option
mode: min, max, sum , avg, median
"sort" : [
{"price" : {"order" : "asc", "mode" : "avg"}}
]
// 4. 数值字段, 强转后排序
// numeric_type, 主要用于跨多个索引搜索
// 但是 排序字段在不同的索引中, 类型不一样
"sort" : [
{
"field" : {"numeric_type" : "double"}
}
]
// date_nanos : 只能表示 1970 - 2262 年的时间, 否则溢出
// 5. 内嵌字段排序
Sorting within nested objects.
// 6. Missing Values - 缺省值
// 当文档不存在的时候, 默认值
_last ,_first , 自定义的值
"sort" : [
{ "price" : {"missing" : "_last"} }
]
// 7 Ignoring Unmapped Fields
如果排序字段 不在 mapping 里面, 会如何? 错误,还是默认行为
// 8. 地理位置排序
Lat Lon as Properties
geohash
Multiple reference points 到多点的距离排序
// 9. Script Based Sorting - 脚本字段排序
// 10. Track Scores - 分数跟踪
默认情况下,排序后,跟踪分数没有意义, 但是您可以设置跟踪分数
// 11. Memory Considerations
排序的字段,会被先加载到内存,您必须有足够的内存去排序
对于字符串字段排序 - 不应该分词, 否则会导致用户意想不到的结果和内存浪费
对于数值字段, 如果可以,建议强转成更小的类型排序
```
网友评论