Elasticsearch - Search APIS

     初学elasticsearch ,翻译了一下Elasticsearch API 中Search 的部分,整理下来,希望对大家有帮助。

Search APIS

Routing

   分片将搜索可以通过提供路由控制参数。例如,当搜索索引信息,路由值可以是用户名:

 

    $ curl -XPOST 'http://localhost:9200/twitter/tweet?routing=kimchy' -d    '{

    "user" : "kimchy",

    "postDate" : "2009-11-15T14:12:12",

     "message" : "trying out Elasticsearch"}'

     在这种情况下,如果我们想只搜索一个特定用户的tweet,我们可以指定它作为路由,只搜索到与之相关的     碎片:

   $ curl-XGET'http://localhost:9200/twitter/tweet/_search?routing=kimchy' -d '{

    "query": {

        "bool" : {

            "must" : {

                "query_string" : {

                    "query" : "some query string here"

                }

            },

            "filter" : {

                "term" : { "user" : "kimchy" }

            }

        }

    }}'

 

Stats Groups

   可以搜索与每组数据聚合、维护相关的统计数组。

    {

    "query" : {

        "match_all" : {}

    },

    "stats" : ["group1", "group2"]}

Global Search Timeout

     个人搜索可以超时请求主体的一部分搜索,因为搜索请求可以来自许多来源,Elasticsearch有动态集群级别设置为全文搜索超时,适用于所有搜索请求在请求正文中不设置一个超时搜索。默认值是没有全文性的超时。设置键search.default_search_timeout和可以使用集群更新设置设置端点。设置这个值-1 重置全文搜索超时没有超时。

   Individual searches can have a timeout as part of theRequest Body Search. Since search requests can originate from many sources, Elasticsearch has a dynamic cluster-level setting for a global search timeout that applies to all search requests that do not set a timeout in the Request Body Search. The default value is no global timeout. The setting key is search.default_search_timeout and can be set using theCluster Update Settings endpoints. Setting this value to -1 resets the global search timeout to no timeout.

 

Search

   执行一个搜索查询并返回与搜索结果匹配的查询。提供的查询可以使用一个简单的查询字符串作为参数,或者使用请求主体。

 

Multi-Index,Multi-Type

  所有的搜索api可以在一个索引应用在多个类型,支持跨过多个索引。

   GET /twitter/_search?q=user:kimchy

     我们也可以搜索特定类型:

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

 我们也可以跨过多个索引:

    GET /kimchy,elasticsearch/tweet/_search?q=tag:wow

 或者使用 _all

 GET /_all/tweet/_search?q=tag:wow

 甚至搜索所有索引和所有类型:

 GET /_search?q=tag:wow

 

URI  Search

执行URI Search搜索仅仅需要提供一个参数。

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

以下是得到的响应:

{

    "timed_out": false,

    "took": 62,

    "_shards":{

        "total" : 1,

        "successful" : 1,

        "failed" : 0

    },

    "hits":{

        "total" : 1,

        "max_score": 1.3862944,

        "hits" : [

            {

                "_index" : "twitter",

                "_type" : "tweet",

                "_id" : "0",

                "_score": 1.3862944,

                "_source" : {

                    "user" : "kimchy",

                    "date" : "2009-11-15T14:12:12",

                    "message" : "trying out Elasticsearch",

                    "likes": 0

                }

            }

        ]

    }}

 

Parameters

URI中允许的参数是:

Name

Description

q

The query string (maps to the query_string query, see Query String Query for more details).查看字符串

df

The default field to use when no field prefix is defined within the query.   没有定义字段前缀,使用默认字段

analyzer

The analyzer name to be used when analyzing the query string.      分析使用查询字符串名称

lowercase_expanded_terms

Should terms be automatically lowercased or not. Defaults to true. 条款应该自动小写的。默认值为true

analyze_wildcard

Should wildcard and prefix queries be analyzed or not. Defaults to false. 通配符和前缀查询应该分析。默认值为false

default_operator

The default operator to be used, can be AND or OR. Defaults to OR. 默认操作符andor默认是 or

lenient

If set to true will cause format based failures (like providing text to a numeric field) to be ignored. Defaults to false. 如果设置为真会导致格式基于故障(如提供文本数字字段)被忽略。默认值为false

explain

For each hit, contain an explanation of how scoring of the hits was computed. 对于每一个打击,含有一个解释的得分的撞击是如何计算的。

_source

Set to false to disable retrieval of the _source field. You can also retrieve part of the document by using _source_include & _source_exclude (see therequest body documentation for more details)设置为false来禁用_source字段的检索。您也可以通过使用_source_include & _source_exclude检索文档的一部分(参见请求主体文档了解更多信息)

stored_fields

The selective stored fields of the document to return for each hit, comma delimited. Not specifying any value will cause no fields to return.选择性存储文档的字段返回对于每一个打击,逗号分隔。不指定任何返回值将导致没有字段。

sort

Sorting to perform. Can either be in the form of fieldName, or fieldName:asc/fieldName:desc. The fieldName can either be an actual field within the document, or the special _score name to indicate sorting based on scores. There can be several sort parameters (order is important).

track_scores

When sorting, set to true in order to still track scores and return them as part of each hit.排序,仍然设置为true,以便跟踪分数和归还每次HIT的一部分。

timeout

A search timeout, bounding the search request to be executed within the specified time value and bail with the hits accumulated up to that point when expired. Defaults to no timeout.

terminate_after

The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early. If set, the response will have a boolean field terminated_early to indicate whether the query execution has actually terminated_early. Defaults to no terminate_after.

from

The starting from index of the hits to return. Defaults to 0.从索引的回归。默认值为0

size

The number of hits to return. Defaults to 10.点击返回的数量。默认为10

search_type

The type of the search operation to perform. Can be dfs_query_then_fetch or query_then_fetch. Defaults to query_then_fetch. SeeSearch Type for more details on the different types of search that can be performed.

 

Request Body Search

  DSL的搜索请求可以执行一个搜索,其中包括body中查询DSL

  GET /twitter/tweet/_search{

    "query" : {

        "term" : { "user" : "kimchy" }

    }}

   这是一个示例的回应:

  {

    "took": 1,

    "timed_out": false,

    "_shards":{

        "total" : 1,

        "successful" : 1,

        "failed" : 0

    },

    "hits":{

        "total" : 1,

        "max_score": 1.3862944,

        "hits" : [

            {

                "_index" : "twitter",

                "_type" : "tweet",

                "_id" : "0",

                "_score": 1.3862944,

                "_source" : {

                    "user" : "kimchy",

                    "message": "trying out Elasticsearch",

                    "date" : "2009-11-15T14:12:12",

                    "likes" : 0

                } }]

    }}

 

Parameters

  

timeout 

A search timeout, bounding the search request to be executed within the specified time value and bail with the hits accumulated up to that point when expired. Defaults to no timeout. Seethe section called “Time unitsedit”.搜索超时,边界内的搜索请求执行指定时间值和保释点击过期时积累到这一点。默认为没有超时

from 

To retrieve hits from a certain offset. Defaults to 0. 检索hits

size 

The number of hits to return. Defaults to 10. If you do not care about getting some hits back but only about the number of matches and/or aggregations, setting the value to 0 will help performance.

search_type 

The type of the search operation to perform. Can be dfs_query_then_fetch or query_then_fetch. Defaults to query_then_fetch. SeeSearch Type for more. 执行搜索操作的类型

request_cache 

Set to true or false to enable or disable the caching of search results for requests where size is 0, ie aggregations and suggestions (no top hits returned). SeeShard request cache.

terminate_after 

The maximum number of documents to collect for each shard, upon reaching which the query execution will terminate early. If set, the response will have a boolean field terminated_early to indicate whether the query execution has actually terminated_early. Defaults to no terminate_after.

 

  Both HTTP GET and HTTP POST can be used to execute search with body. Since not all clients support GET with body, POST is allowed as well.

 

Fast check for any matching docs

如果我们只是想知道如果有任何文档匹配一个特定的查询,我们可以设置大小为0,表明我们不感兴趣的搜索结果。我们也可以设置terminate_after 1表明可以终止时第一个匹配的查询执行文件被发现(每个碎片)

 

 GET /_search?q=message:elasticsearch&size=0&terminate_after=1

 

响应:

 

{

  "took": 3,

  "timed_out": false,

  "terminated_early": true,

  "_shards": {

    "total": 1,

    "successful": 1,

    "failed": 0

  },

  "hits": {

    "total": 1,

    "max_score": 0.0,

    "hits": []

  }}

 

Query

允许定义一个查询使用查询DSL搜索请求身体内的查询元素。

GET /_search{

    "query" : {

        "term" : { "user" : "kimchy" }

    }}

From/Size

     分页的结果可以通过from size 参数查询,from 定义了从第一个结果你想取回抵消,size允许允许您配置的最大点击返回。

    Though from and size can be set as request parameters, they can also be set within the search body. from defaults to 0, and size defaults to 10.

 

 GET /_search{

    "from" : 0, "size" : 10,

    "query" : {

        "term" : { "user" : "kimchy" }

    }}

 

Sort

允许添加一个或者更多sort到特定的字段。每个类到可以被翻转。The sort is defined on a per field level, with special field name for _score to sort by score, and _doc to sort by index order.

 

假设以下指数映射:

PUT /my_index{

    "mappings": {

        "my_type": {

            "properties": {

                "post_date": { "type": "date" },

                "user": {

                    "type": "keyword"

                },

                "name": {

                    "type": "keyword"

                },

                "age": { "type": "integer" }

            }

        }

    }}

GET /my_index/my_type/_search{

    "sort" : [

        { "post_date" : {"order" : "asc"}},

        "user",

        { "name" : "desc" },

        { "age" : "desc" },

        "_score"

    ],

    "query" : {

        "term" : { "user" : "kimchy" }

    }}

 

_doc没有真正用例除了最有效的排序顺序

如果你不关心文件返回的顺序,那么你应该按_doc排序。当滚动的时候有特别帮助。

Sort Values

The sort values for each document returned are also returned as part of the response.排序值为每个文档返回的响应。

Sort Order

The order option can have the following values:

 

 

 

asc

Sort in ascending order  升序排序

desc

Sort in descending order   降序排序

Sort mode option

Elasticsearch支持排序的数组或多值字段,模式选择可以有以下值:

 

min

Pick the lowest value.选择最小值

max

Pick the highest value. 选择最高(大)值

sum

Use the sum of all values as sort value. Only applicable for number based array fields. 使用sort值作为所有值的总和,只适用于基于数字数组字段。

avg

Use the average of all values as sort value. Only applicable for number based array fields. 使用所有值的平均值作为排序值。只适用于基于数字数组字段。

median

Use the median of all values as sort value. Only applicable for number based array fields. 使用中位数的值作为排序值。只适用于基于数字数组字段。

Sort mode example usage

In the example below the field price has multiple prices per document. In this case the result hits will be sorted by price ascending based on the average price per document.

在下面的例子中每个文档多个价格。在这种情况下,将排序结果点击价格提升基于每个文档的平均价格。

PUT /my_index/my_type/1?refresh

{

   "product": "chocolate",

    "price": [20, 4]

}

 

POST /_search

{

   "query" : {

      "term" : { "product" : "chocolate" }

   },

   "sort" : [

      {"price" : {"order" : "asc", "mode" : "avg"}}

   ]

}

 

Sorting within nested objects

Elasticsearch also supports sorting by fields that are inside one or more nested objects. The sorting by nested field support has the following parameters on top of the already existing sort options:

Elasticsearch还支持在一个或多个排序的字段嵌套对象。嵌套的排序字段支持以下参数在现有排序选项:

nested_path

Defines on which nested object to sort. The actual sort field must be a direct field inside this nested object. When sorting by nested field, this field is mandatory.

定义的嵌套对象进行排序。实际的字段必须直接字段排序在这个嵌套对象。当嵌套分类字段,这个字段是强制性的。

nested_filter

A filter that the inner objects inside the nested path should match with in order for its field values to be taken into account by sorting. Common case is to repeat the query / filter inside the nested filter or query. By default no nested_filter is active.

过滤器内部对象内嵌套的路径应配以为了其字段值来考虑排序。常见的情况是重复查询/过滤器内嵌套的过滤器或查询。默认情况下没有nested_filter活跃

Nested sorting example

In the below example  offer  is a field of type   nested. The nested_path needs to be specified; otherwise, elasticsearch doesn’t know on what nested level sort values need to be captured.

在以下示例中提供一个嵌套类型的字段。需要指定nested_path;否则,elasticsearch不知道什么需要捕捉嵌套级别排序值。

 

 

POST /_search

{

   "query" : {

      "term" : { "product" : "chocolate" }

   },

   "sort" : [

       {

          "offer.price" : {

             "mode" :  "avg",

             "order" : "asc",

             "nested_path" : "offer",

             "nested_filter" : {

                "term" : { "offer.color" : "blue" }

             }

          }

       }

    ]

}

Missing Values

The missing parameter specifies how docs which are missing the field should be treated: Themissing value can be set to _last_first, or a custom value (that will be used for missing docs as the sort value). For example:

指定文档丢失的参数的失踪字段应该如何处理:

GET /_search

{

    "sort" : [

        { "price" : {"missing" : "_last"} }

    ],

    "query" : {

        "term" : { "product" : "chocolate" }

    }

}

Source filtering

Allows to control how the _source field is returned with every hit. 可以控制如何每一hit_source字段返回

To disable _source retrieval set to false: 禁用_source检索设置为假:

GET /_search

{

    "_source": false,

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

The _source also accepts one or more wildcard patterns to control what parts of the _source should be returned:

_source还接受一个或多个通配符模式控制_source的哪些部分应该被归还:

GET /_search

{

    "_source": "obj.*",

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

或者

GET /_search

{

    "_source": [ "obj1.*", "obj2.*" ],

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

Finally, for complete control, you can specify both includes and excludes patterns:

GET /_search

{

    "_source": {

        "includes": [ "obj1.*", "obj2.*" ],

        "excludes": [ "*.description" ]

    },

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

 

 

 

Fields

明确stored_fields参数是关于字段标记为存储在映射中,默认情况下是关闭的,一般不推荐。使用源过滤而不是选择返回原始源文档的子集。

GET /_search

{

    "stored_fields" : ["user", "postDate"],

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

can be used to load all stored fields from the document.

对于每个hit空数组将导致只有_id_type返回,例如:

GET /_search

{

    "stored_fields" : [],

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

disable stored fields entirely 

To disable the stored fields (and metadata fields) entirely use: \_none_:

GET /_search

{

    "stored_fields": "_none_",

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

Script Fields

Allows to return a script evaluation (based on different fields) for each hit, for example:

GET /_search

{

    "query" : {

        "match_all": {}

    },

    "script_fields" : {

        "test1" : {

            "script" : {

                "lang": "painless",

                "inline": "doc['my_field_name'].value * 2"

            }

        },

        "test2" : {

            "script" : {

                "lang": "painless",

                "inline": "doc['my_field_name'].value * factor",

                "params" : {

                    "factor"  : 2.0

                }

            }

        }

    }

}

Script fields can also access the actual _source document indexed and extract specific elements to be returned from it (can be an "object" type). 

脚本还可以访问字段的实际_source文档索引并从中提取出特定元素返回(可以是一个对象类型)

GET /_search

    {

        "query" : {

            "match_all": {}

        },

        "script_fields" : {

            "test1" : {

                "script" : "_source.obj1.obj2"

            }

        }

    }

Doc value Fields

Allows to return the doc value representation of a field for each hit, for example:

允许返回的doc值表示为每个字段

GET /_search

{

    "query" : {

        "match_all": {}

    },

    "docvalue_fields" : ["test1", "test2"]

}

Post filter

The post_filter is applied to the search hits at the very end of a search request, after aggregations have already been calculated.

 post_filter应用于搜索热门的搜索请求,聚合后已经计算

卖衬衫的有以下属性:

PUT /shirts

{

    "mappings": {

        "item": {

            "properties": {

                "brand": { "type": "keyword"},

                "color": { "type": "keyword"},

                "model": { "type": "keyword"}

            }

        }

    }

}

 

PUT /shirts/item/1?refresh

{

    "brand": "gucci",

    "color": "red",

    "model": "slim"

}

Imagine a user has specified two filters: 想象一个用户指定的两个过滤器:

GET /shirts/_search

{

  "query": {

    "bool": {

      "filter": [

        { "term": { "color": "red"   }},

        { "term": { "brand": "gucci" }}

      ]

    }

  }

}

可以用聚合:

GET /shirts/_search

{

  "query": {

    "bool": {

      "filter": [

        { "term": { "color": "red"   }},

        { "term": { "brand": "gucci" }}

      ]

    }

  },

  "aggs": {

    "models": {

      "terms": { "field": "model" } 

    }

  }

}

 返回最受欢迎的库奇红衬衫

您想要包括所有颜色的衬衫在聚合过程中,然后把颜色过滤搜索结果。这是post_filter的目的:

GET /shirts/_search

{

  "query": {

    "bool": {

      "filter": {

        "term": { "brand": "gucci" } 

      }

    }

  },

  "aggs": {

    "colors": {

      "terms": { "field": "color" } 

    },

    "color_red": {

      "filter": {

        "term": { "color": "red" } 

      },

      "aggs": {

        "models": {

          "terms": { "field": "model" } 

        }

      }

    }

  },

  "post_filter": { 

    "term": { "color": "red" }

  }

}

主查询现在发现所有古奇的衬衫,无论颜色。.

查询古奇颜色流行的衬衫。,

 

 color_red agg限制 models sub-aggregation to red Gucci shirts.

最后,post_filter从搜索中删除红色以外的颜色 

 

Highlighting

Allows to highlight search results on one or more fields.

可以突出显示搜索结果在一个或多个字段

GET /_search

{

    "query" : {

        "match": { "user": "kimchy" }

    },

    "highlight" : {

        "fields" : {

            "content" : {}

        }

    }

}

更详细信息:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html

 

Rescoring

有助于改善精度通过重新排序(100 - 500)查询返回的文档和post_filter阶段,使用二次(通常更昂贵)算法,代替昂贵的算法应用到所有文件的索引。

Query Rescorer

curl -s-XPOST 'localhost:9200/_search' -d '{

   "query" : {

      "match" : {

         "field1" : {

            "operator" : "or",

            "query" : "the quick brown",

            "type" : "boolean"

         }

      }

   },

   "rescore" : {

      "window_size" : 50,

      "query" : {

         "rescore_query" : {

            "match" : {

               "field1" : {

                  "query" : "the quick brown",

                  "type" : "phrase",

                  "slop" : 2

               }

            }

         },

         "query_weight" : 0.7,

         "rescore_query_weight" : 1.2

      }

   }

}

'

The way the scores are combined can be controlled with the score_mode:

scores相结合的方式可以控制score_mode:

Search Type

有不同的执行路径,可以当执行分布式搜索完成。分布式搜索操作需要分散到所有相关的分片,然后收集的所有结果。在散射/收集类型执行,有几种方法可以做到这一点,特别是与搜索引擎。

Preference

控制优先分片副本执行的搜索请求

_primary

操作只会去执行主分片

_primary_first

操作在主分片去执行,如果没有可用的(故障转移),将执行在其他分片

_replica

操作只会去执行一个复制分片

_replica_first

操作在副分片去执行,如果没有可用的(故障转移),将执行在其他分片

_local

如果可能的话要执行的操作将更喜欢在本地分片

_prefer_nodes:abc,xyz

如果适用更喜欢提供的节点与节点上执行id(abcxyz在这种情况下)

_shards:2,3

限制了操作指定的分片(23)。此首选项可以结合其他偏好却出现第一:_shards:23 | _primary

_only_nodes

限制节点中指定节点的操作规范

https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html

 

 

 

 

例如,使用用户的会话ID,以确保一致的排序结果为用户: 

GET /_search?preference=xyzabc123

{

    "query": {

        "match": {

            "title": "elasticsearch"

        }

    }

}

Explain

可以解释为每个击中它的分数是如何计算的

GET /_search

{

    "explain": true,

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

 

Version

Returns a version for each search hit.

GET /_search

{

    "version": true,

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

Index Boost

 

Allows to configure different boost level per index when searching across more than one indices. This is very handy when hits coming from one index matter more than hits coming from another index (think social graph where each user has an index).

允许配置不同的boost level每跨多个指标索引搜索时。这个非常方便,当点击来自指数比打击来自另一个更重要索引(每个用户都有一个索引)

GET /_search

{

    "indices_boost" : {

        "index1" : 1.4,

        "index2" : 1.3

    }

}

min_score

Exclude documents which have a _score less than the minimum specified in min_score: 

排除的文件在min_score _score小于指定的最低:

GET /_search

{

    "min_score": 0.5,

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

:大多数时候,这并没有多大意义,但提供了先进的用例

Search After

Pagination of results can be done by using the from and size but the cost becomes prohibitive when the deep pagination is reached.

分页的结果可以使用和大小,但当深分页成本就太高,

 The index.max_result_window which defaults to 10,000 is a safeguard, search requests take heap memory and time proportional to from + size.

索引max_result_window默认为10000是一个保障,搜索请求从+堆内存和时间成比例的大小

GET twitter/tweet/_search

{

    "size": 10,

    "query": {

        "match" : {

            "title" : "elasticsearch"

        }

    },

    "sort": [

        {"date": "asc"},

        {"_uid": "desc"}

    ]

}

例如我们可以用最后一个文档的排序值并将其传递给search_after检索下一个页面的结果:

GET twitter/tweet/_search

{

    "size": 10,

    "query": {

        "match" : {

            "title" : "elasticsearch"

        }

    },

    "search_after": [1463538857, "tweet#654323"],

    "sort": [

        {"date": "asc"},

        {"_uid": "desc"}

    ]

}

Search Template

The /_search/template endpoint allows to use the mustache language to pre render search requests, before they are executed and fill existing templates with template parameters.

/ _search /模板端点允许使用the mustache language前呈现搜索请求,执行之前和填补现有模板与模板参数。

GET /_search/template

{

    "inline" : {

      "query": { "match" : { "{{my_field}}" : "{{my_value}}" } },

      "size" : "{{my_size}}"

    },

    "params" : {

        "my_field" : "foo",

        "my_value" : "bar",

        "my_size" : 5

    }

}

Converting parameters to JSON

The {{#toJson}}parameter{{/toJson}} function can be used to convert parameters like maps and array to their JSON representation:

{ { # toJson } } { { / toJson } }函数参数可以用来转换参数和数组的JSON表示:

GET /_search/template

{

  "inline": "{ \"query\": { \"terms\": { \"status\": {{#toJson}}status{{/toJson}} }}}",

  "params": {

    "status": [ "pending", "published" ]

  }

}

这呈现为:

{

  "query": {

    "terms": {

      "status": [

        "pending",

        "published"

      ]

    }

  }

}

一个更复杂的例子替代品JSON对象数组:

{

    "inline": "{\"query\":{\"bool\":{\"must\": {{#toJson}}clauses{{/toJson}} }}}",

    "params": {

        "clauses": [

            { "term": "foo" },

            { "term": "bar" }

        ]

   }

}

呈现为

{

    "query" : {

      "bool" : {

        "must" : [

          {

            "term" : "foo"

          },

          {

            "term" : "bar"

          }

        ]

      }

    }

}

 

注:搜索模板特别重要以上只是举了一个转换为JSON的例子 具体说明请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-search-template.html

 

 

Search Shards API

The search shards api returns the indices and shards that a search request would be executed against. This can give useful feedback for working out issues or planning optimizations with routing and shard preferences. 

搜索api返回索引,分片搜索请求将被执行。这可以为工作提供有用的反馈问题或计划优化路由和分片的偏好

索引和类型参数可能是单一值,或逗号分隔。

Usage

GET /twitter/_search_shards

 

返回结果:

{

  "nodes": ...,

  "shards": [

    [

      {

        "index": "twitter",

        "node": "JklnKbD7Tyqi9TP3_Q_tBg",

        "primary": true,

        "shard": 0,

        "state": "STARTED",

        "allocation_id": {"id":"0TvkCyF7TAmM1wHP4a42-A"},

        "relocating_node": null

      }

    ],

    [

      {

        "index": "twitter",

        "node": "JklnKbD7Tyqi9TP3_Q_tBg",

        "primary": true,

        "shard": 1,

        "state": "STARTED",

        "allocation_id": {"id":"fMju3hd1QHWmWrIgFnI4Ww"},

        "relocating_node": null

      }

    ],

    [

      {

        "index": "twitter",

        "node": "JklnKbD7Tyqi9TP3_Q_tBg",

        "primary": true,

        "shard": 2,

        "state": "STARTED",

        "allocation_id": {"id":"Nwl0wbMBTHCWjEEbGYGapg"},

        "relocating_node": null

      }

    ],

    [

      {

        "index": "twitter",

        "node": "JklnKbD7Tyqi9TP3_Q_tBg",

        "primary": true,

        "shard": 3,

        "state": "STARTED",

        "allocation_id": {"id":"bU_KLGJISbW0RejwnwDPKw"},

        "relocating_node": null

      }

    ],

    [

      {

        "index": "twitter",

        "node": "JklnKbD7Tyqi9TP3_Q_tBg",

        "primary": true,

        "shard": 4,

        "state": "STARTED",

        "allocation_id": {"id":"DMs7_giNSwmdqVukF7UydA"},

        "relocating_node": null

      }

    ]

  ]

}

并指定相同的请求,这一次路由值:

GET /twitter/_search_shards?routing=foo,baz

返回:

{

  "nodes": ...,   此处有省略.

  "shards": [

    [

      {

        "index": "twitter",

        "node": "JklnKbD7Tyqi9TP3_Q_tBg",

        "primary": true,

        "shard": 0,

        "state": "STARTED",

        "allocation_id": {"id":"0TvkCyF7TAmM1wHP4a42-A"},

        "relocating_node": null

      }

    ],

    [

      {

        "index": "twitter",

        "node": "JklnKbD7Tyqi9TP3_Q_tBg",

        "primary": true,

        "shard": 1,

        "state": "STARTED",

        "allocation_id": {"id":"fMju3hd1QHWmWrIgFnI4Ww"},

        "relocating_node": null

      }

    ]

  ]

}

所有参数:

routing:一个以逗号分隔的路由值来考虑决定哪些分片请求时执行.

Preference控制偏好的分片副本执行的搜索请求。默认情况下,分片之间的操作是随机副本。

Local一个布尔值是否读取本地集群状态以确定分片分配而不是使用主节点的集群状态.

Suggesters

Suggest特性表明看起来相似的条款提供基于文本使用方式。部分显示功能仍在发展中.

POST twitter/_search

{

  "query" : {

    "match": {

      "message": "tring out Elasticsearch"

    }

  },

  "suggest" : {

    "my-suggestion" : {

      "text" : "trying out Elasticsearch",

      "term" : {

        "field" : "message"

      }

    }

  }

}

 

suggest请求对_suggest端点执行应该忽略周围的suggestelement只用在显示一个搜索请求的一部分.

POST _suggest

{

  "my-suggestion" : {

    "text" : "tring out Elasticsearch",

    "term" : {

      "field" : "message"

    }

  }

}

注:更多详情:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html

Multi Search API

The multi search API allows to execute several search requests within the same API. The endpoint for it is _msearch.

允许在相同的API执行几个搜索请求。这是_msearch的端点。

请求的格式类似于大部分API的格式:

header\n

body\n

header\n

body\n

 

$ cat requests

{"index" : "test"}

{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}

{"index" : "test", "search_type" : "dfs_query_then_fetch"}

{"query" : {"match_all" : {}}}

{}

{"query" : {"match_all" : {}}}

 

{"query" : {"match_all" : {}}}

{"search_type" : "dfs_query_then_fetch"}

{"query" : {"match_all" : {}}}

 

$ curl -XGET localhost:9200/_msearch--data-binary"@requests"; echo

 

注意,上面包含一个空的标题的一个例子(也可以没有任何内容)的支持。

端点也允许搜索索引/索引类型/类型的URI本身,在这种情况下,它将被用作默认除非显式定义的头

$ cat requests

{}

{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}

{}

{"query" : {"match_all" : {}}}

{"index" : "test2"}

{"query" : {"match_all" : {}}}

 

$ curl -XGET localhost:9200/test/_msearch--data-binary@requests; echo

 

Count API

计数API允许轻松地执行一个查询,查询匹配的数量。它可以跨越一个或多个指标和执行一个或多个类型。提供的查询可以使用一个简单的查询字符串作为参数,或使用查询请求体中定义DSL.

PUT /twitter/tweet/1?refresh

{

    "user": "kimchy"

}

 

GET /twitter/tweet/_count?q=user:kimchy

 

GET /twitter/tweet/_count

{

    "query" : {

        "term" : { "user" : "kimchy" }

    }

}

上面两个例子做同样的事情,其结果是

{

    "count" : 1,

    "_shards" : {

        "total" : 5,

        "successful" : 5,

        "failed" : 0

    }

}

Request Parameters请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html

 

 

 

以上只是一些看到的APIS写了一些翻译及总结,具体详细信息请参考官方网站。

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值