1,最佳字段
dis_max 查詢
(分離最大化查詢,Disjunction Max Query):將任何與任一查詢匹配的文檔作為結果返回,但只將最佳匹配的評分作為查詢的評分結果返回;
// 1,初始化數據
PUT /my_index/my_type/1
{
"title": "Quick brown rabbits",
"body": "Brown rabbits are commonly seen."
}
PUT /my_index/my_type/2
{
"title": "Keeping pets healthy",
"body": "My quick brown fox eats rabbits on a regular basis."
}
// 2,dis_max 查詢
GET /my_index/my_type/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "Brown fox" }},
{ "match": { "body": "Brown fox" }}
]
}
}
}
2,multi_match
查詢
multi_match
查詢為能在多個字段上反復執行相同查詢提供了一種便捷方式;
// 初始版本
{
"dis_max": {
"queries": [
{
"match": {
"title": {
"query": "Quick brown fox",
"minimum_should_match": "30%"
}
}
},
{
"match": {
"body": {
"query": "Quick brown fox",
"minimum_should_match": "30%"
}
}
},
],
"tie_breaker": 0.3
}
}
// multi_match 簡潔形式
{
"multi_match":{
"query":"Quick brown fox",
"type":"best_fields",
"fields":["title","body"],
"tie_breaker":0.3,
"minimum_should_match":"30%"
}
}
3,多數字段
// 1,多字段映射
DELETE /my_index
PUT /my_index
{
"settings": { "number_of_shards": 1 },
"mappings": {
"my_type": {
"properties": {
"title": {
"type": "text",
"analyzer": "english",
"fields": {
"std": {
"type": "text",
"analyzer": "standard"
}
}
}
}
}
}
}
// 2, 初始化數據
PUT /my_index/my_type/1
{ "title": "My rabbit jumps" }
PUT /my_index/my_type/2
{ "title": "Jumping jack rabbits" }
// 3,簡單搜索
GET /my_index/_search
{
"query": {
"match": {
"title": "jumping rabbits"
}
}
}
// 3.1 multi_match 搜索
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "jumping rabbits",
"type": "most_fields",
"fields": [ "title", "title.std" ]
}
}
}
// 3.2 通過boost自定義最終評分貢獻
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "jumping rabbits",
"type": "most_fields",
"fields": [ "title^10", "title.std" ]
}
}
}
4,cross-fields
跨字段查詢
// 字段中心式的 most_fields 查詢的 explanation
GET /_validate/query?explain
{
"query":{
"multi_match": {
"query": "peter smith",
"type": "most_fields",
"operator": "and",
"fields":["first_name", "last_name"]
}
}
}
// 詞中心式的 cross_fields 查詢的 explanation
GET /_validate/query?explain
{
"query":{
"multi_match": {
"query": "peter smith",
"type": "cross_fields",
"operator": "and",
"fields":["first_name", "last_name"]
}
}
}
**參考資料:** -[多字段搜索](https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-field-search.html)