##搜索選項
一些查詢字符串(query-string)可選參數能夠影響搜索過程。
####preference(偏愛)
`preference`參數允許你控制使用哪個分片或節點來處理搜索請求。她接受如下一些參數 `_primary`, `_primary_first`, `_local`, `_only_node:xyz`, `_prefer_node:xyz`和`_shards:2,3`。這些參數在文檔[搜索偏好(search preference)](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-preference.html)里有詳細描述。
然而通常最有用的值是一些隨機字符串,它們可以避免結果震蕩問題(the _bouncing results_ problem)。
#####結果震蕩(Bouncing Results)
* 想像一下,你正在按照`timestamp`字段來對你的結果排序,并且有兩個document有相同的timestamp。由于搜索請求是在所有有效的分片副本間輪詢的,這兩個document可能在原始分片里是一種順序,在副本分片里是另一種順序。
* 這就是被稱為_結果震蕩(bouncing results)_的問題:用戶每次刷新頁面,結果順序會發生變化。避免這個問題方法是對于同一個用戶總是使用同一個分片。方法就是使用一個隨機字符串例如用戶的會話ID(session ID)來設置`preference`參數。
###timeout(超時)
通常,協調節點會等待接收所有分片的回答。如果有一個節點遇到問題,它會拖慢整個搜索請求。
`timeout`參數告訴協調節點最多等待多久,就可以放棄等待而將已有結果返回。返回部分結果總比什么都沒有好。
搜索請求的返回將會指出這個搜索是否超時,以及有多少分片成功答復了:
``` js
...
"timed_out": true, (1)
"_shards": {
"total": 5,
"successful": 4,
"failed": 1 (2)
},
...
```
--------------------------------------------------
(1) 搜索請求超時。
(2) 五個分片中有一個沒在超時時間內答復。
如果一個分片的所有副本都因為其他原因失敗了——也許是因為硬件故障——這個也同樣會反映在該答復的`_shards`部分里。
### routing(路由選擇)
在路由值那節里,我們解釋了如何在建立索引時提供一個自定義的`routing`參數來保證所有相關的document(如屬于單個用戶的document)被存放在一個單獨的分片中。在搜索時,你可以指定一個或多個`routing` 值來限制只搜索那些分片而不是搜索index里的全部分片:
``` js
GET /_search?routing=user_1,user2
```
這個技術在設計非常大的搜索系統時就會派上用場了。我們在規模(scale)那一章里詳細討論它。
### search_type(搜索類型)
雖然`query_then_fetch`是默認的搜索類型,但也可以根據特定目的指定其它的搜索類型,例如:
``` js
GET /_search?search_type=count
```
___count(計數)___
`count(計數)`搜索類型只有一個`query(查詢)`的階段。當不需要搜索結果只需要知道滿足查詢的document的數量時,可以使用這個查詢類型。
___query_and_fetch(查詢并且取回)___
`query_and_fetch(查詢并且取回)`搜索類型將查詢和取回階段合并成一個步驟。這是一個內部優化選項,當搜索請求的目標只是一個分片時可以使用,例如指定了`routing(路由選擇)`值時。雖然你可以手動選擇使用這個搜索類型,但是這么做基本上不會有什么效果。
___dfs_query_then_fetch___ 和 ___dfs_query_and_fetch___
`dfs`搜索類型有一個預查詢的階段,它會從全部相關的分片里取回項目頻數來計算全局的項目頻數。我們將在relevance-is-broken(相關性被破壞)里進一步討論這個。
___scan(掃描)___
`scan(掃描)`搜索類型是和`scroll(滾屏)`API連在一起使用的,可以高效地取回巨大數量的結果。它是通過禁用排序來實現的。我們將在下一節_scan-and-scroll(掃描和滾屏)_里討論它。
<!--
=== Search Options
A few ((("search options")))optional query-string parameters can influence the search process.
==== preference
The `preference` parameter allows((("preference parameter")))((("search options", "preference"))) you to control which shards or nodes are
used to handle the search request. It accepts values such as `_primary`,
`_primary_first`, `_local`, `_only_node:xyz`, `_prefer_node:xyz`, and
`_shards:2,3`, which are explained in detail on the
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-preference.html[search `preference`]
documentation page.
However, the most generally useful value is some arbitrary string, to avoid
the _bouncing results_ problem.((("bouncing results problem")))
[[bouncing-results]]
.Bouncing Results
****
Imagine that you are sorting your results by a `timestamp` field, and
two documents have the same timestamp. Because search requests are
round-robined between all available shard copies, these two documents may be
returned in one order when the request is served by the primary, and in
another order when served by the replica.
This is known as the _bouncing results_ problem: every time the user refreshes
the page, the results appear in a different order. The problem can be avoided by always using the same shards for the same user,
which can be done by setting the `preference` parameter to an arbitrary string
like the user's session ID.
****
==== timeout
By default, the coordinating node waits((("search options", "timeout"))) to receive a response from all shards.
If one node is having trouble, it could slow down the response to all search
requests.
The `timeout` parameter tells((("timeout parameter"))) the coordinating node how long it should wait
before giving up and just returning the results that it already has. It can be
better to return some results than none at all.
The response to a search request will indicate whether the search timed out and
how many shards responded successfully:
[source,js]
--------------------------------------------------
...
"timed_out": true, <1>
"_shards": {
"total": 5,
"successful": 4,
"failed": 1 <2>
},
...
--------------------------------------------------
<1> The search request timed out.
<2> One shard out of five failed to respond in time.
If all copies of a shard fail for other reasons--perhaps because of a
hardware failure--this will also be reflected in the `_shards` section of
the response.
[[search-routing]]
==== routing
In <<routing-value>>, we explained how a custom `routing` parameter((("search options", "routing")))((("routing parameter"))) could be
provided at index time to ensure that all related documents, such as the
documents belonging to a single user, are stored on a single shard. At search
time, instead of searching on all the shards of an index, you can specify
one or more `routing` values to limit the search to just those shards:
[source,js]
--------------------------------------------------
GET /_search?routing=user_1,user2
--------------------------------------------------
This technique comes in handy when designing very large search systems, and we
discuss it in detail in <<scale>>.
[[search-type]]
==== search_type
While `query_then_fetch` is the default((("query_then_fetch search type")))((("search options", "search_type")))((("search_type"))) search type, other search types can
be specified for particular purposes, for example:
[source,js]
--------------------------------------------------
GET /_search?search_type=count
--------------------------------------------------
`count`::
The `count` search type has only a `query` phase.((("count search type"))) It can be used when you
don't need search results, just a document count or
<<aggregations,aggregations>> on documents matching the query.
`query_and_fetch`::
The `query_and_fetch` search type ((("query_and_fetch serch type")))combines the query and fetch phases into a
single step. This is an internal optimization that is used when a search
request targets a single shard only, such as when a
<<search-routing,`routing`>> value has been specified. While you can choose
to use this search type manually, it is almost never useful to do so.
`dfs_query_then_fetch` and `dfs_query_and_fetch`::
The `dfs` search types((("dfs search types"))) have a prequery phase that fetches the term
frequencies from all involved shards in order to calculate global term
frequencies. We discuss this further in <<relevance-is-broken>>.
`scan`::
The `scan` search type is((("scan search type"))) used in conjunction with the `scroll` API ((("scroll API")))to
retrieve large numbers of results efficiently. It does this by disabling
sorting. We discuss _scan-and-scroll_ in the next section.
-->
- 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