ES系列五、ES6.3常用api之搜索类api
1.搜索api
1.1.routing:路由
执行搜索时,它将广播到所有索引/索引分片(副本之间的循环)。可以通过提供routing参数来控制将搜索哪些分片。例如,在索引book时,路由值可以是name。
POST book/english?routing=test
{
"name":"test",
"age":"1",
"book":"zhegnsh1正式"
}
按路由查询:
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_selection从false到true
PUT /_cluster/settings
{
"transient": {
"cluster.routing.use_adaptive_replica_selection": true
}
}
1.3.Stats Groups:统计组
搜索可以与统计组相关联,统计组维护每个组的统计聚合。稍后可以使用indices stats 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": ,
    "timed_out": false,
    "_shards": {
        "total": ,
        "successful": ,
        "skipped": ,
        "failed":
    },
    "hits": {
        "total": ,
        "max_score": 4.181759,
        "hits": [
            {
                "_index": "book",
                "_type": "english",
                "_id": "nwmH_mQBbhSmAk-T97Mf",
                "_score": 4.181759,
                "_source": {
                    "name": "bb传交换机发个沙发覆盖否",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            }
        ]
    }
}
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
其他参数解释如下:
| 
 
  | 
 查询字符串(映射到  | 
| 
 
  | 
 在查询中未定义字段前缀时使用的默认字段。  | 
| 
 
  | 
 分析查询字符串时要使用的分析器名称。  | 
| 
 
  | 
 是否应分析通配符和前缀查询。默认为  | 
| 
 
  | 
 应在协调节点上一次减少的分片结果数。如果请求中潜在的分片数量很大,则应将此值用作保护机制,以减少每个搜索请求的内存开销。  | 
| 
 
  | 
 要使用的默认运算符可以是  | 
| 
 
  | 
 如果设置为true将导致忽略基于格式的失败(如向数字字段提供文本)。默认为false。  | 
| 
 
  | 
 对于每个命中,包含如何计算命中得分的解释。  | 
| 
 
  | 
 设置为  | 
| 
 
  | 
 每个匹配返回的文档的选择性存储字段,逗号分隔。不指定任何值将导致没有字段返回。  | 
| 
 
  | 
 排序执行。可以是  | 
| 
 
  | 
 排序时,设置为  | 
| 
 
  | 
 设置为  | 
| 
 
  | 
 搜索超时,将搜索请求限制在指定的时间值内执行,并使用在到期时累积的点击数进行保释。默认为无超时。  | 
| 
 
  | 
 在达到查询执行将提前终止时,为每个分片收集的最大文档数。如果设置,响应将具有一个布尔字段,  | 
| 
 
  | 
 从命中的索引开始返回。默认为  | 
| 
 
  | 
 要返回的点击次数。默认为  | 
| 
 
  | 
 要执行的搜索操作的类型。可以是   | 
| 
 
  | 
 
  | 
官方文档参考:Search Api
3.Request body search:带参数条件搜索
3.1.query term搜索整个词
POST book1/_search
{
"query" : {
"term" : { "name" : "test goog money" }
}
}
response
{
    "took": ,
    "timed_out": false,
    "_shards": {
        "total": ,
        "successful": ,
        "skipped": ,
        "failed":
    },
    "hits": {
        "total": ,
        "max_score": null,
        "hits": []
    }
}
由于默认分词器把“test goog my money”,分成了三个单词,所有没有匹配到。
3.2.query match分词后搜索
post book1/_search
{
"query": {
"match":{
"name":"test goog money"
} }
}
response
{
    "took": ,
    "timed_out": false,
    "_shards": {
        "total": ,
        "successful": ,
        "skipped": ,
        "failed":
    },
    "hits": {
        "total": ,
        "max_score": 11.610666,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": 11.610666,
                "_source": {
                    "name": "test goog my money",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": ""
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": ""
                }
            }
        ]
    }
}
3.3.query_string分词后匹配任何字段
{
    "query": {
        "query_string":{
            "query":"test goog my money 国"
        }
    }
}
response
{
    "took": ,
    "timed_out": false,
    "_shards": {
        "total": ,
        "successful": ,
        "skipped": ,
        "failed":
    },
    "hits": {
        "total": ,
        "max_score": 11.610666,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": 11.610666,
                "_source": {
                    "name": "test goog my money",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "lAmG_mQBbhSmAk-T-bN1",
                "_score": 2.016771,
                "_source": {
                    "name": "国1里",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": ""
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": 1.8562036,
                "_source": {
                    "name": "test",
                    "age": ""
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "jgmG_mQBbhSmAk-TnrMw",
                "_score": 1.5432179,
                "_source": {
                    "name": "国国",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "kgmG_mQBbhSmAk-T6bOW",
                "_score": 1.5067708,
                "_source": {
                    "name": "国1里国",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国1"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "kwmG_mQBbhSmAk-T8bN7",
                "_score": 1.5067708,
                "_source": {
                    "name": "国1里国",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "mgmH_mQBbhSmAk-TbbMX",
                "_score": 0.18232156,
                "_source": {
                    "name": "里个覆盖否",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "mwmH_mQBbhSmAk-TerNv",
                "_score": 0.18232156,
                "_source": {
                    "name": "里个盖否",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "ngmH_mQBbhSmAk-T6LPZ",
                "_score": 0.13353139,
                "_source": {
                    "name": "cvh交换机发个沙发覆盖否",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                }
            }
        ]
    }
}
参数说明:
| 
 
  | 
|
| 
 
  | 
 从某个偏移量中检索命中。默认为  | 
| 
 
  | 
 要返回的点击次数。默认为  | 
| 
 
  | 
 要执行的搜索操作的类型。可以是   | 
| 
 
  | 
 设置为  | 
| 
 
  | 
 
  | 
| 
 
  | 
 在达到查询执行将提前终止时,为每个分片收集的最大文档数。如果设置,响应将具有一个布尔字段,  | 
| 
 
  | 
 应在协调节点上一次减少的分片结果数。如果请求中潜在的分片数量很大,则应将此值用作保护机制,以减少每个搜索请求的内存开销。  | 
出了上述情况,search_type,request_cache和allow_partial_search_results 设置必须作为查询字符串参数传递。搜索请求的其余部分应该在正文中传递。正文内容也可以作为名为的REST参数传递source。
HTTP GET和HTTP POST都可用于使用body执行搜索。由于并非所有客户端都支持使用正文GET,因此也允许使用POST。
3.4 From / Size:分页
POST _search
{
"from" : , "size" : ,
"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}}}}
在对其进行排序时,顺序默认为desc_score,在对其他任何内容进行排序时,默认为asc。
也可以多个字段排序
{
     "sort" : [
        { "name" : {"order" : "desc"}}, // 此写法与下面的写法等价
        { "age" : "desc" },
        "_score"
    ],
    "query": {
        "term":{
            "name":"国"
        }
    }
}
4.1.数组字段排序
Elasticsearch支持按数组或多值字段进行排序。该mode选项控制选择哪个数组值以对其所属的文档进行排序。该mode选项可以具有以下值:
| 
 
  | 
 选择最低值。  | 
| 
 
  | 
 选择最高价值。  | 
| 
 
  | 
 使用所有值的总和作为排序值。仅适用于基于数字的数组字段。  | 
| 
 
  | 
 使用所有值的平均值作为排序值。仅适用于基于数字的数组字段。  | 
| 
 
  | 
 使用所有值的中位数作为排序值。仅适用于基于数字的数组字段。  | 
示例:
POST book1/_search
{
"sort" : [
{"age" : {"order" : "asc", "mode" : "avg"}}
],
"query": {
"term":{
"name":"test"
} }
}
response:
{
    "took": ,
    "timed_out": false,
    "_shards": {
        "total": ,
        "successful": ,
        "skipped": ,
        "failed":
    },
    "hits": {
        "total": ,
        "max_score": null,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": null,
                "_source": {
                    "name": "test",
                    "age": ""
                },
                "sort": [
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": null,
                "_source": {
                    "name": "test",
                    "age": ""
                },
                "sort": [
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": null,
                "_source": {
                    "name": "test goog my money",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                },
                "sort": [
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "54UiUmUBRzBxBrDgfIl9",
                "_score": null,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        ,
                        ,
                    ],
                    "class": "dsfdsf",
                    "addr": "中国"
                },
                "sort": [
                ]
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "6IUkUmUBRzBxBrDgFok2",
                "_score": null,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        ,
                        ,
                        ,
                    ],
                    "class": "dsfdsf",
                    "addr": "中国"
                },
                "sort": [
                ]
            }
        ]
    }
}
4.2嵌套查询排序
该字段必须是嵌套字段才行
POST /_search
{
"query" : {
"term" : { "product" : "chocolate" }
},
"sort" : [
{
"offer.price" : {
"mode" : "avg",
"order" : "asc",
"nested": {
"path": "offer",
"filter": {
"term" : { "offer.color" : "blue" }
}
}
}
}
]
}
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" : [-, ],
"order" : "asc",
"unit" : "km",
"mode" : "min",
"distance_type" : "arc"
}
}
],
"query" : {
"term" : { "user" : "kimchy" }
}
}
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": ,
    "timed_out": false,
    "_shards": {
        "total": ,
        "successful": ,
        "skipped": ,
        "failed":
    },
    "hits": {
        "total": ,
        "max_score": 2.9026666,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": 2.9026666,
                "fields": {
                    "test1": [
                    ],
                    "test2": [
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "6IUkUmUBRzBxBrDgFok2",
                "_score": 2.1818507,
                "fields": {
                    "test1": [
                    ],
                    "test2": [
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": 1.5205609,
                "fields": {
                    "test1": [
                    ],
                    "test2": [
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "",
                "_score": 1.5205609,
                "fields": {
                    "test1": [
                    ],
                    "test2": [
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "54UiUmUBRzBxBrDgfIl9",
                "_score": 1.0615592,
                "fields": {
                    "test1": [
                    ],
                    "test2": [
                    ]
                }
            }
        ]
    }
}
4.7.1控制返回字段
POST book1/_search
{
"script_fields" : {
"test1" : {
"script" : "params['_source']['addr']"
}
},
"query": {
"term":{
"name":"test"
} }
}
请注意_source此处的关键字以导航类似json的模型。
理解之间的区别是很重要的 doc['my_field'].value和params['_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": ,
    "timed_out": false,
    "_shards": {
        "total": ,
        "successful": ,
        "skipped": ,
        "failed":
    },
    "hits": {
        "total": ,
        "max_score": ,
        "hits": [
            {
                "_index": "book1",
                "_type": "english",
                "_id": "6IUkUmUBRzBxBrDgFok2",
                "_score": ,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        ,
                        ,
                        ,
                    ],
                    "class": "dsfdsf",
                    "addr": "中国"
                },
                "fields": {
                    "name": [
                        "goog",
                        "money",
                        "my",
                        "test"
                    ],
                    "addr": [
                        "中",
                        "国"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "54UiUmUBRzBxBrDgfIl9",
                "_score": ,
                "_source": {
                    "name": "test goog my money",
                    "age": [
                        ,
                        ,
                    ],
                    "class": "dsfdsf",
                    "addr": "中国"
                },
                "fields": {
                    "name": [
                        "goog",
                        "money",
                        "my",
                        "test"
                    ],
                    "addr": [
                        "中",
                        "国"
                    ]
                }
            },
            {
                "_index": "book1",
                "_type": "english",
                "_id": "5oVDQ2UBRzBxBrDgtIl0",
                "_score": ,
                "_source": {
                    "name": "test goog my money",
                    "age": ,
                    "class": "dsfdsf",
                    "addr": "中国"
                },
                "fields": {
                    "name": [
                        "goog",
                        "money",
                        "my",
                        "test"
                    ],
                    "addr": [
                        "中",
                        "国"
                    ]
                }
            }
        ]
    }
}
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>否"
                    ]
                }
            }
        ]
    }
}
默认<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。分段器
指定如何在高亮片段中分解文本:simple或span。仅适用于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_fields,phrase_limit 会考虑每个匹配字段的短语。提高限制会增加查询时间并消耗更多内存。仅由fvh荧光笔支持。默认为256。
require_field_match:默认情况下,仅突出显示包含查询匹配的字段。设置require_field_match为false突出显示所有字段。默认为true。
tags_schema:设置为styled使用内置标记架构。该styled 架构定义了如下的pre_tags并定义post_tags为</em>。
type:unified,plain或fvh。默认为 unified。
5.3.hightlighter类型
Elasticsearch支持三种hightlighter:unified,plain和fvh(快速矢量荧光笔)。可以指定type要为每个字段使用的突出显示器。
unified
该unified荧光笔使用Lucene的统一hightlighter。这个hightlighter将文本分成句子,并使用BM25算法对单个句子进行评分,就好像它们是语料库中的文档一样。它还支持准确的短语和多项(模糊,前缀,正则表达式)突出显示。这是默认的hightlighter。
plain
该plain hightlighter使用标准Lucene的hightlighter。它试图在词汇查询中理解单词重要性和任何单词定位标准方面反映查询匹配逻辑。
该plain hightlighter最适合在单一field突出简单的查询匹配。为了准确反映查询逻辑,它会创建一个微小的内存中索引,并通过Lucene的查询执行计划程序重新运行原始查询条件,以访问当前文档的低级别匹配信息。对每个字段和需要突出显示的每个文档重复此操作。如果要在复杂查询的大量文档中突出显示很多字段,我们建议使用unified hightlighter postings或term_vector字段。
fvh
该fvh荧光笔使用Lucene的快速hightlighter。此突出显示器可用于映射中term_vector设置为的 字段with_positions_offsets。
- 需要设置
term_vector以with_positions_offsets增加索引的大小 - 可以将来自多个字段的匹配组合成一个结果。看到 
matched_fields - 可以为不同位置的匹配分配不同的权重,允许在突出显示提升词组匹配的提升查询时,将词组匹配等术语排序在术语匹配之上
 
ES系列五、ES6.3常用api之搜索类api的更多相关文章
- kafka系列五、kafka常用java API
		
引入maven包 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka- ...
 - springboot系列五、springboot常用注解使用说明
		
一.controller相关注解 1.@Controller 控制器,处理http请求. 2.@RespController Spring4之后新加的注解,原来返回json需要@ResponseBod ...
 - ES系列(五):获取单条数据get处理过程实现
		
前面讲的都是些比较大的东西,即框架层面的东西.今天咱们来个轻松点的,只讲一个点:如题,get单条记录的es查询实现. 1. get语义说明 get是用于搜索单条es的数据,是根据主键id查询数据方式. ...
 - ES系列目录
		
ES系列一.CentOS7安装ES 6.3.1 ES系列二.CentOS7安装ES head6.3.1 ES系列三.基本知识准备 ES系列四.ES6.3常用api之文档类api ES系列五.ES6.3 ...
 - API 设计 POSIX File API
		
小结: 1. https://mp.weixin.qq.com/s/qWrSyzJ54YEw8sLCxAEKlA API 设计最佳实践的思考 谷朴 阿里技术 昨天 阿里妹导读:API 是模块或者子 ...
 - ES系列十五、ES常用Java Client API
		
一.简介 1.先看ES的架构图 二.ES支持的客户端连接方式 1.REST API http请求,例如,浏览器请求get方法:利用Postman等工具发起REST请求:java 发起httpClien ...
 - ES系列四、ES6.3常用api之文档类api
		
1.Index API: 创建并建立索引 PUT twitter/tweet/ { "user" : "kimchy", "post_date&quo ...
 - ES6系列之项目中常用的新特性
		
ES6系列之项目中常用的新特性 ES6常用特性 平时项目开发中灵活运用ES6+语法可以让开发者减少很多开发时间,提高工作效率.ES6版本提供了很多新的特性,接下来我列举项目中常用的ES6+的特性: l ...
 - 【HANA系列】【第五篇】SAP HANA XS的JavaScript API详解
		
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第五篇]SAP HANA XS ...
 
随机推荐
- 牛客练习赛 小D的剑阵 解题报告
			
小D的剑阵 题意链接: https://ac.nowcoder.com/acm/contest/369/F 来源:牛客网 现在你有 \(n\) 把灵剑,其中选择第i把灵剑会得到的 \(w_i\) 攻击 ...
 - JS数组冒泡排序&去重
			
冒泡排序: var a = [2,1,4,3,6,5]; for(var d = 0 ; d< a.length; d++){ for(var b = d+1; b < a.length; ...
 - USACO Section 1.1
			
这是4道大水题. 因为我看有些题解写的很丑陋,就把我的代码发上来. 第一题是我早期作品,丑陋不堪...... #include <cstdio> #include <iostream ...
 - dijkstra(最短路)和Prim(最小生成树)下的堆优化
			
dijkstra(最短路)和Prim(最小生成树)下的堆优化 最小堆: down(i)[向下调整]:从第k层的点i开始向下操作,第k层的点与第k+1层的点(如果有)进行值大小的判断,如果父节点的值大于 ...
 - Linux下,根据FHS定义出来的每个目录的作用
			
(下表摘自<鸟哥的Linux的私房菜>) 在Linux下,根据FHS定义出来的每个目录应该放置的档案内容为: 目录 应放置档案内容 / 根目录 root (/),一般建议在根目录底下只接目 ...
 - 理解 PHP 依赖注入
			
Laravel框架的依赖注入确实很强大,并且通过容器实现依赖注入可以有选择性的加载需要的服务,减少初始化框架的开销,下面是我在网上看到的一个帖子,写的很好拿来与大家分享,文章从开始按照传统的类设计数据 ...
 - 基于 Dojo toolkit 实现 web2.0 的 MVC 模式
			
前言 MVC 模式是设计模式中的经典模式,它可以有效的分离数据层,展示层,和业务逻辑层.Web2.0 技术由于其良好的用户体验被广泛应用于 WEB 应用的展示层.但是在传统的 web 开发中,展示层的 ...
 - 增加/删除临时IP地址
			
启动一个down状态的网卡并设置临时IP: ifconfig eth3 172.168.1.100 netmask 255.255.255.0 up 删除网卡的临时IP地址: ip addr del ...
 - python---memcache基本使用以及内部原理
			
简单使用: import memcache mc = memcache.Client(['127.0.0.1:8081','127.0.0.1:8082','127.0.0.1:8083','127. ...
 - golang数组排序算法
			
冒泡排序 图 https://www.cnblogs.com/onepixel/articles/7674659.html package main import ( "fmt" ...