ES系列5、ES6.3經常使用api之搜索類api

1.搜索api

1.1.routing:路由

執行搜索時,它將廣播到全部索引/索引分片(副本之間的循環)。能夠經過提供routing參數來控制將搜索哪些分片。例如,在索引book時,路由值能夠是name。html

POST book/english?routing=testgit

{
  "name":"test",
  "age":"1",
  "book":"zhegnsh1正式"
}github

按路由查詢:正則表達式

POST book/_search?routing=test
{
    "query": { "bool" : { "must" : { "query_string" : { "query" : "good" } }, "filter" : { "term" : { "name" : "test" } } } } }
 "query" : "good" 全文搜索模糊匹配,返回任何字段包含"good」的記錄。

1.2.adaptive replica selection:自適應副本選擇

做爲以循環方式發送到數據副本的請求的替代方法,能夠啓用自適應副本選擇。這容許協調節點根據許多標準將請求發送到被認爲「最佳」的副本:算法

  • 協調節點與包含數據副本的節點之間的過去請求的響應時間
  • 超過搜索請求的時間在包含數據的節點上執行
  • 包含數據的節點上的搜索線程池的隊列大小

這能夠經過改變所述動態羣集配置開啓 cluster.routing.use_adaptive_replica_selectionfalsetrue
json

PUT /_cluster/settings
{
    "transient": {
        "cluster.routing.use_adaptive_replica_selection": true
    }
}

1.3.Stats Groups:統計組

搜索能夠與統計組相關聯,統計組維護每一個組的統計聚合。稍後可使用indices stats API專門檢索它 。例如,如下是將請求與兩個不一樣的組相關聯的搜索正文請求:api

POST /_search
{
    "query" : { "match_all" : {} }, "stats" : ["group1", "group2"] }

 

1.4.全局搜索超時

做爲請求正文搜索的一部分,單個搜索可能會超時 。因爲搜索請求能夠源自多個源,所以Elasticsearch具備全局搜索超時的動態集羣級設置,適用於未在請求正文搜索中設置超時的全部搜索請求。默認值爲無全局超時。search.default_search_timeout可使用「 羣集更新設置」端點設置和設置設置密鑰。設置此值可-1將全局搜索超時重置爲無超時。數組

1.5.搜索取消

可使用標準任務取消 機制取消搜索。默認狀況下,正在運行的搜索僅檢查是否在段邊界上取消它,所以取消可能會被大段延遲。經過將動態集羣級別設置設置search.low_level_cancellation爲,能夠提升搜索取消響應性true。可是,它帶來了更頻繁的取消檢查的額外開銷,這在大型快速運行的搜索查詢中是顯而易見的。更改此設置僅影響更改後開始的搜索。緩存

1.6.搜索併發和並行

默認狀況下,Elasticsearch不會根據請求命中的分片數拒絕任何搜索請求。雖然Elasticsearch將優化協調節點上的搜索執行,但大量分片會對CPU和內存產生重大影響。以這樣的方式組織數據一般是一個更好的主意,即更少的大分片。若是您要配置軟限制,能夠更新action.search.shard_count.limit 羣集設置以拒絕搜索過多分片的搜索請求。架構

request參數max_concurrent_shard_requests可用於控制搜索API將爲請求執行的最大併發分片請求數。此參數應用於保護單個請求不會使羣集過載(例如,默認請求將命中羣集中的全部索引,若是每一個節點的分片數量很高,則可能致使碎片請求被拒絕)。此默認值基於羣集中的數據節點數,但最多256

2.multi-index,multi-type:多索引,多類型搜索

2.1.單個索引的全部類型

GET /book/_search?q=name:bb

返回name字段包含‘bb’的全部文檔(模糊查詢)

response:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 4.181759,
        "hits": [
            {
                "_index": "book",
                "_type": "english",
                "_id": "nwmH_mQBbhSmAk-T97Mf",
                "_score": 4.181759,
                "_source": {
                    "name": "bb傳交換機發個沙發覆蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            }
        ]
    }
}
View Code

2.2.搜索特定類型

GET /twitter/tweet,user/_search?q=user:kimchy

2.3.搜索多個索引

GET book1,book/_search?q=name:bb

2.4.搜索全部索引

GET _all/_search?q=name:bb

或者

GET _search?q=name:bb
其餘參數解釋以下:

q

查詢字符串(映射到query_string查詢,有關詳細信息,請參閱 查詢字符串查詢)。

df

在查詢中未定義字段前綴時使用的默認字段。

analyzer

分析查詢字符串時要使用的分析器名稱。

analyze_wildcard

是否應分析通配符和前綴查詢。默認爲false

batched_reduce_size

應在協調節點上一次減小的分片結果數。若是請求中潛在的分片數量很大,則應將此值用做保護機制,以減小每一個搜索請求的內存開銷。

default_operator

要使用的默認運算符能夠是AND或 OR默認爲OR

lenient

若是設置爲true將致使忽略基於格式的失敗(如向數字字段提供文本)。默認爲false。

explain

對於每一個命中,包含如何計算命中得分的解釋。

_source

設置爲false禁用_source字段檢索您還可使用_source_include檢索部分文檔_source_exclude 有關詳細信息,請參閱請求正文文檔)

stored_fields

每一個匹配返回的文檔的選擇性存儲字段,逗號分隔。不指定任何值將致使沒有字段返回。

sort

排序執行。能夠是fieldName或 fieldName:asc的形式fieldName:descfieldName能夠是文檔中的實際字段,也能夠是特殊_score名稱,表示基於分數的排序。能夠有幾個sort參數(順序很重要)。

track_scores

排序時,設置爲true仍然跟蹤分數並將其做爲每一個匹配的一部分返回。

track_total_hits

設置爲false禁用跟蹤與查詢匹配的匹配總數。有關詳細信息,請參閱索引排序)。默認爲true。

timeout

搜索超時,將搜索請求限制在指定的時間值內執行,並使用在到期時累積的點擊數進行保釋。默認爲無超時。

terminate_after

在達到查詢執行將提早終止時,爲每一個分片收集的最大文檔數。若是設置,響應將具備一個布爾字段,terminated_early以指示查詢執行是否實際上已終止。默認爲no terminate_after。

from

從命中的索引開始返回。默認爲0

size

要返回的點擊次數。默認爲10

search_type

要執行的搜索操做的類型。能夠是 dfs_query_then_fetchquery_then_fetch默認爲query_then_fetch有關能夠執行的不一樣搜索類型的更多詳細信息,請參閱 搜索類型

allow_partial_search_results

false若是請求將產生部分結果,則設置爲返回總體故障。默認爲true,這將容許在超時或部分失敗的狀況下得到部分結果。

官方文檔參考:Search Api

3.Request body search:帶參數條件搜索

3.1.query term搜索整個詞

POST book1/_search
{
    "query" : {
        "term" : { "name" : "test goog money" }
    }
}

response

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}
View Code

因爲默認分詞器把「test goog my money」,分紅了三個單詞,全部沒有匹配到。

3.2.query match分詞後搜索

post book1/_search
{
    "query": {
        "match":{
            "name":"test goog money"
        }
        
    }
}

response

{
    "took": 17,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 11.610666,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": 11.610666,
                "_source": {
                    "name": "test goog my money",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "32",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": "1"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "33",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": "1"
                }
            }
        ]
    }
}
View Code

3.3.query_string分詞後匹配任何字段

{
    "query": {
        "query_string":{
            "query":"test goog my money 國"
        }
        
    }
}

response

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 22,
        "max_score": 11.610666,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": 11.610666,
                "_source": {
                    "name": "test goog my money",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "lAmG_mQBbhSmAk-T-bN1",
                "_score": 2.016771,
                "_source": {
                    "name": "國1裏",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "32",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": "1"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "33",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": "1"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "jgmG_mQBbhSmAk-TnrMw",
                "_score": 1.5432179,
                "_source": {
                    "name": "國國",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "kgmG_mQBbhSmAk-T6bOW",
                "_score": 1.5067708,
                "_source": {
                    "name": "國1裏國",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國1"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "kwmG_mQBbhSmAk-T8bN7",
                "_score": 1.5067708,
                "_source": {
                    "name": "國1裏國",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "mgmH_mQBbhSmAk-TbbMX",
                "_score": 0.18232156,
                "_source": {
                    "name": "裏個覆蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "mwmH_mQBbhSmAk-TerNv",
                "_score": 0.18232156,
                "_source": {
                    "name": "裏個蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "ngmH_mQBbhSmAk-T6LPZ",
                "_score": 0.13353139,
                "_source": {
                    "name": "cvh交換機發個沙發覆蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                }
            }
        ]
    }
}
View Code

參數說明:

timeout

搜索超時,將搜索請求限制在指定的時間值內執行,並使用在到期時累積的點擊數進行保釋。默認爲無超時。請參閱時間單位編輯

from

從某個偏移量中檢索命中。默認爲0

size

要返回的點擊次數。默認爲10若是您不關心某些匹配,但只關注匹配和/或聚合的數量,將值設置爲0有助於提升性能。

search_type

要執行的搜索操做的類型。能夠是 dfs_query_then_fetchquery_then_fetch默認爲query_then_fetch請參閱搜索類型以獲取更多

request_cache

設置爲truefalse啓用或禁用對於size爲0的請求的搜索結果的緩存,即聚合和建議(未返回頂部命中)。請參閱Shard請求緩存

allow_partial_search_results

false若是請求將產生部分結果,則 設置爲返回總體故障。默認爲true,這將在超時或部分失敗的狀況下容許部分結果。

terminate_after

在達到查詢執行將提早終止時,爲每一個分片收集的最大文檔數。若是設置,響應將具備一個布爾字段,terminated_early以指示查詢執行是否實際上已終止。默認爲no terminate_after。

batched_reduce_size

應在協調節點上一次減小的分片結果數。若是請求中潛在的分片數量很大,則應將此值用做保護機制,以減小每一個搜索請求的內存開銷。

出了上述狀況,search_typerequest_cacheallow_partial_search_results 設置必須做爲查詢字符串參數傳遞。搜索請求的其他部分應該在正文中傳遞。正文內容也能夠做爲名爲的REST參數傳遞source

HTTP GET和HTTP POST均可用於使用body執行搜索。因爲並不是全部客戶端都支持使用正文GET,所以也容許使用POST。

3.4 From / Size:分頁

POST _search
{
    "from" : 0, "size" : 2,
    "query": {
        "query_string":{
            "query":"test goog my money國"
        }
        
    }
}

默認:"from" : 0, "size" : 10

4.Sort:排序

{
     "sort" : [
        { "name" : "desc" },
        "_score"
    ],
    "query": {
        "term":{
            "name":""
        }
        
    }
}

 若是報錯:

"reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [class] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            

開啓該字段  fielddata=true :

PUT /book1/_mapping/english/?pretty

{"english":{"properties":{"name":{"type":"text","fielddata":true}}}}

在對其_score進行排序時,順序默認爲desc,在對其餘任何內容進行排序時,默認爲asc

也能夠多個字段排序

{
     "sort" : [
        { "name" : {"order" : "desc"}}, // 此寫法與下面的寫法等價
        { "age" : "desc" },
        "_score"
    ],
    "query": {
        "term":{
            "name":""
        }
        
    }
}
View Code

4.1.數組字段排序

Elasticsearch支持按數組或多值字段進行排序。該mode選項控制選擇哪一個數組值以對其所屬的文檔進行排序。該mode選項能夠具備如下值:

min

選擇最低值。

max

選擇最高價值。

sum

使用全部值的總和做爲排序值。僅適用於基於數字的數組字段。

avg

使用全部值的平均值做爲排序值。僅適用於基於數字的數組字段。

median

使用全部值的中位數做爲排序值。僅適用於基於數字的數組字段。

示例:

POST book1/_search
{
     "sort" : [
         {"age" : {"order" : "asc", "mode" : "avg"}}
    ],
    "query": {
        "term":{
            "name":"test"
        }
        
    }
}

response:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": null,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "32",
                "_score": null,
                "_source": {
                    "name": "test",
                    "age": "1"
                },
                "sort": [
                    1
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "33",
                "_score": null,
                "_source": {
                    "name": "test",
                    "age": "1"
                },
                "sort": [
                    1
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": null,
                "_source": {
                    "name": "test goog my money",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "sort": [
                    12
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "54UiUmUBRzBxBrDgfIl9",
                "_score": null,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        11,
                        13,
                        14
                    ],
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "sort": [
                    13
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "6IUkUmUBRzBxBrDgFok2",
                "_score": null,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        14,
                        54,
                        45,
                        34
                    ],
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "sort": [
                    37
                ]
            }
        ]
    }
}
View Code

4.2嵌套查詢排序

該字段必須是嵌套字段才行

POST /_search
{
   "query" : {
      "term" : { "product" : "chocolate" }
   },
   "sort" : [
       {
          "offer.price" : {
             "mode" :  "avg",
             "order" : "asc",
             "nested": {
                "path": "offer",
                "filter": {
                   "term" : { "offer.color" : "blue" }
                }
             }
          }
       }
    ]
}
View Code

4.3缺失值

missing參數指定如何其缺乏字段文檔應被視爲:該missing值能夠被設置爲_last_first或自定義的值(將被用於缺乏文檔做爲排序值)。默認是_last

4.4.忽略未映射的字段

默認狀況下,若是沒有與字段關聯的映射,搜索請求將失敗。unmapped_type選項容許忽略沒有映射但不按其排序的字段。此參數的值用於肯定要發出的排序值。如下是如何使用它的示例:

GET /_search
{
    "sort" : [
        { "price" : {"unmapped_type" : "long"} }
    ],
    "query" : {
        "term" : { "product" : "chocolate" }
    }
}

4.5.地理距離排序

  容許排序依據 _geo_distance 這是一個例子,假設 pin.location 是一個類型的字段 geo_point
GET /_search
{
    "sort" : [
        {
            "_geo_distance" : {
                "pin.location" : [-70, 40],
                "order" : "asc",
                "unit" : "km",
                "mode" : "min",
                "distance_type" : "arc"
            }
        }
    ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

更多參考官方API

4.6._source:控制返回的字段

GET /_search
{
    "_source": {
        "includes": [ "obj1.*", "obj2.*" ],
        "excludes": [ "*.description" ]
    },
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

返回匹配includes的,去除匹配excludes的字段!

4.7.Script Field:修改返回的字段

POST book1/_search

{
"script_fields" : { "test1" : { "script" : { "lang": "painless", "source": "doc['age'].value * 2" } }, "test2" : { "script" : { "lang": "painless", "source": "doc['age'].value * params.factor", "params" : { "factor" : 2.0 } } } }, "query": { "term":{ "name":"test" } } }

response:

{
    "took": 135,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 2.9026666,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": 2.9026666,
                "fields": {
                    "test1": [
                        24
                    ],
                    "test2": [
                        24
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "6IUkUmUBRzBxBrDgFok2",
                "_score": 2.1818507,
                "fields": {
                    "test1": [
                        28
                    ],
                    "test2": [
                        28
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "32",
                "_score": 1.5205609,
                "fields": {
                    "test1": [
                        2
                    ],
                    "test2": [
                        2
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "33",
                "_score": 1.5205609,
                "fields": {
                    "test1": [
                        2
                    ],
                    "test2": [
                        2
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "54UiUmUBRzBxBrDgfIl9",
                "_score": 1.0615592,
                "fields": {
                    "test1": [
                        22
                    ],
                    "test2": [
                        22
                    ]
                }
            }
        ]
    }
}
View Code

4.7.1控制返回字段

POST book1/_search
{
     "script_fields" : {
            "test1" : {
                "script" : "params['_source']['addr']"
            }
        },
    "query": {
        "term":{
            "name":"test"
        }
        
    }
}

請注意_source此處關鍵字以導航相似json的模型。

理解之間的區別是很重要的 doc['my_field'].valueparams['_source']['my_field']第一個,使用doc關鍵字,將致使該字段的術語加載到內存(緩存),這將致使更快的執行,但更多的內存消耗。此外,doc[...]符號僅容許簡單的值字段(您不能從中返回json對象),而且僅對非分析或基於單個術語的字段有意義。可是,doc若是可能的話,仍然是使用文檔中值的推薦方法,由於_source必須在每次使用時加載和解析。使用_source很是慢。

4.8.Doc Value Ffields:返回匹配文檔的全部分詞

POST /book1/_search
{
    "query": {
        "bool":{
            "filter":[
                {"term":{"name":"test"}},
                 { "term":{"addr":""}}
                ]
        }
        
    },
     "docvalue_fields" : ["name", "addr"]
}

repsonse:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "6IUkUmUBRzBxBrDgFok2",
                "_score": 0,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        14,
                        54,
                        45,
                        34
                    ],
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "fields": {
                    "name": [
                        "goog",
                        "money",
                        "my",
                        "test"
                    ],
                    "addr": [
                        "",
                        ""
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "54UiUmUBRzBxBrDgfIl9",
                "_score": 0,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        11,
                        13,
                        14
                    ],
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "fields": {
                    "name": [
                        "goog",
                        "money",
                        "my",
                        "test"
                    ],
                    "addr": [
                        "",
                        ""
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": 0,
                "_source": {
                    "name": "test goog my money",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "fields": {
                    "name": [
                        "goog",
                        "money",
                        "my",
                        "test"
                    ],
                    "addr": [
                        "",
                        ""
                    ]
                }
            }
        ]
    }
}
View Code

Doc Value和Ffields的更多瞭解:ES-正排索Doc Values和Field Data

4.9 Post Filter:聚合過濾

在已經計算聚合以後,post_filter其應用於hits搜索請求最末端的搜索

POST /shirts/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": { "brand": "gucci" } //詢如今能夠找到Gucci的全部襯衫,不管顏色如何
      }
    }
  },
  "aggs": {
    "colors": {
      "terms": { "field": "color" } //返回流行的(出如今文檔最多頻率的顏色)的Gucci的全部襯衫
    },
    "color_red": {
      "filter": {
        "term": { "color": "red" } // 顏色爲紅色的Gucci的全部襯衫
      },
      "aggs": {
        "models": {
          "terms": { "field": "model" } // 最流行的款式(出如今文檔最多頻率的顏色)的Gucci的全部襯衫
        }
      }
    }
  },
  "post_filter": { 
    "term": { "color": "red" } // 搜索中刪除紅色之外的顏色
  }
}的記錄

 

 5.HighLighting:搜索結果高亮顯示

複製代碼
POST book1/_search

{
    "query":{
         "bool": {
          "filter": [
            { "term": { "name": "裏"   }},
            { "term": { "age": "12" }}
          ]
        }
    },
"highlight" : {
        "fields" : {
            "name" : {"type" : "plain"}
        }
    }
}
複製代碼

response:

{
    "took": 77,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 11,
        "max_score": 0,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "mgmH_mQBbhSmAk-TbbMX",
                "_score": 0,
                "_source": {
                    "name": "裏個覆蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "<em>裏</em>個覆蓋否"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "mwmH_mQBbhSmAk-TerNv",
                "_score": 0,
                "_source": {
                    "name": "裏個蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "<em>裏</em>個蓋否"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "lAmG_mQBbhSmAk-T-bN1",
                "_score": 0,
                "_source": {
                    "name": "國1裏",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "國1<em>裏</em>"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "lQmH_mQBbhSmAk-TDrNt",
                "_score": 0,
                "_source": {
                    "name": "裏",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "<em>裏</em>"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "lwmH_mQBbhSmAk-TObNH",
                "_score": 0,
                "_source": {
                    "name": "裏fgsaf覆蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "<em>裏</em>fgsaf覆蓋否"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "mAmH_mQBbhSmAk-TTbO2",
                "_score": 0,
                "_source": {
                    "name": "裏jhj發個沙發覆蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "<em>裏</em>jhj發個沙發覆蓋否"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "kgmG_mQBbhSmAk-T6bOW",
                "_score": 0,
                "_source": {
                    "name": "國1裏國",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國1"
                },
                "highlight": {
                    "name": [
                        "國1<em>裏</em>國"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "kwmG_mQBbhSmAk-T8bN7",
                "_score": 0,
                "_source": {
                    "name": "國1裏國",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "國1<em>裏</em>國"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "nAmH_mQBbhSmAk-Tg7OW",
                "_score": 0,
                "_source": {
                    "name": "裏蓋否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "<em>裏</em>蓋否"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "nQmH_mQBbhSmAk-TkbP4",
                "_score": 0,
                "_source": {
                    "name": "裏否",
                    "age": 12,
                    "class": "dsfdsf",
                    "addr": "中國"
                },
                "highlight": {
                    "name": [
                        "<em>裏</em>否"
                    ]
                }
            }
        ]
    }
}
View Code

默認<em></em>標籤包裹,也能夠自定義標籤,例如:<span></span>

5.1.自定義標籤

複製代碼
{
    "query":{
         "bool": {
          "filter": [
            { "term": { "name": "裏"   }},
            { "term": { "age": "12" }}
          ]
        }
    },
"highlight" : {
        "pre_tags" : ["<span>"],
        "post_tags" : ["</span>"],
        "fields" : {
            "name" : {"type" : "plain"}
        }
    }
}
複製代碼

5.2.控制高亮區域和返回片斷數量

複製代碼
{
    "query":{
         "match": {
          "name":"the 裏"
        }
    },
"highlight" : {
        "pre_tags" : ["<tag1>"],
        "post_tags" : ["</tag1>"],
        "type": "plain",
         "fragment_size" : 20,
         "number_of_fragments" : 5,
         "fields" : {
             "name":{}
        }
            
    }
}
複製代碼

force_source:即便字段單獨存儲,也會根據源突出顯示。默認爲false分段器

指定如何在高亮片斷中分解文本:simplespan。僅適用於plain熒光筆。默認爲span

simple
將文本分解爲相同大小的片斷。
span
將文本分解爲相同大小的片斷,但試圖避免在突出顯示的術語之間分解文本,默認。

fragment_offset控制要開始突出顯示的邊距。僅在使用fvh熒光筆時有效。fragment_size突出顯示的片斷的大小(以字符爲單位)默認爲100。

 

matched_fields:在多個字段上組合匹配以突出顯示單個字段。對於以不一樣方式分析相同字符串的多字段,這是最直觀的。全部matched_fields必須term_vector設置爲 with_positions_offsets,但只加載組合匹配的字段,所以只有該字段從store設置爲受益 yes。僅適用於fvh熒光筆。

no_match_size:若是沒有要突出顯示的匹配片斷,則要從字段開頭返回的文本量。默認爲0(不返回任何內容)。

number_of_fragments:要返回的最大片斷數。若是片斷數設置爲0,則不返回任何片斷。而是突出顯示並返回整個字段內容。當您須要突出顯示標題或地址等短文本時,這可能很方便,但不須要分段。若是number_of_fragments 爲0,fragment_size則忽略。默認爲5。

order:設置爲時按排名突出顯示片斷score。默認狀況下,片斷將按照它們在字段中出現的順序輸出(順序:) none。將此選項設置爲score將首先輸出最相關的片斷。每一個熒光筆都應用本身的邏輯來計算相關性分數。有關 不一樣熒光筆如何找到最佳碎片的更多詳細信息,請參閱文檔高亮顯示器如何在內部工做

phrase_limit:控制考慮的文檔中匹配短語的數量。防止fvh熒光筆分析太多短語並消耗太多內存。使用時matched_fieldsphrase_limit 會考慮每一個匹配字段的短語。提升限制會增長查詢時間並消耗更多內存。僅由fvh熒光筆支持。默認爲256。

require_field_match:默認狀況下,僅突出顯示包含查詢匹配的字段。設置require_field_matchfalse突出顯示全部字段。默認爲true

tags_schema:設置爲styled使用內置標記架構。該styled 架構定義了以下的pre_tags並定義post_tags</em>

typeunifiedplainfvh。默認爲 unified

5.3.hightlighter類型

Elasticsearch支持三種hightlighter:unifiedplainfvh(快速矢量熒光筆)。能夠指定type要爲每一個字段使用的突出顯示器。

unified

unified熒光筆使用Lucene的統一hightlighter。這個hightlighter將文本分紅句子,並使用BM25算法對單個句子進行評分,就好像它們是語料庫中的文檔同樣。它還支持準確的短語和多項(模糊,前綴,正則表達式)突出顯示。這是默認的hightlighter。

plain

plain hightlighter使用標準Lucene的hightlighter。它試圖在詞彙查詢中理解單詞重要性和任何單詞定位標準方面反映查詢匹配邏輯。

plain hightlighter最適合在單一field突出簡單的查詢匹配。爲了準確反映查詢邏輯,它會建立一個微小的內存中索引,並經過Lucene的查詢執行計劃程序從新運行原始查詢條件,以訪問當前文檔的低級別匹配信息。對每一個字段和須要突出顯示的每一個文檔重複此操做。若是要在複雜查詢的大量文檔中突出顯示不少字段,咱們建議使用unified hightlighter postingsterm_vector字段。

fvh

fvh熒光筆使用Lucene的快速hightlighter。此突出顯示器可用於映射中term_vector設置爲的 字段with_positions_offsets。

  • 須要設置term_vectorwith_positions_offsets增長索引的大小
  • 能夠未來自多個字段的匹配組合成一個結果。看到 matched_fields
  • 能夠爲不一樣位置的匹配分配不一樣的權重,容許在突出顯示提高詞組匹配的提高查詢時,將詞組匹配等術語排序在術語匹配之上
相關文章
相關標籤/搜索