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_selectionfalsetrue

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": 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
  • 可以为不同位置的匹配分配不同的权重,允许在突出显示提升词组匹配的提升查询时,将词组匹配等术语排序在术语匹配之上

posted on 2018-08-15 12:43  小人物的奋斗  阅读(5764)  评论(0编辑  收藏  举报

导航