mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-06 21:18:31 +00:00
a03b6c2fa5
This commit adds back "id" as the key within a script to specify a stored script (which with file scripts now gone is no longer ambiguous). It also adds "source" as a replacement for "code". This is in an attempt to normalize how scripts are specified across both put stored scripts and script usages, including search template requests. This also deprecates the old inline/stored keys.
67 lines
1.6 KiB
Plaintext
67 lines
1.6 KiB
Plaintext
[[mapping-index-field]]
|
|
=== `_index` field
|
|
|
|
When performing queries across multiple indexes, it is sometimes desirable to
|
|
add query clauses that are associated with documents of only certain indexes.
|
|
The `_index` field allows matching on the index a document was indexed into.
|
|
Its value is accessible in `term`, or `terms` queries, aggregations,
|
|
scripts, and when sorting:
|
|
|
|
NOTE: The `_index` is exposed as a virtual field -- it is not added to the
|
|
Lucene index as a real field. This means that you can use the `_index` field
|
|
in a `term` or `terms` query (or any query that is rewritten to a `term`
|
|
query, such as the `match`, `query_string` or `simple_query_string` query),
|
|
but it does not support `prefix`, `wildcard`, `regexp`, or `fuzzy` queries.
|
|
|
|
[source,js]
|
|
--------------------------
|
|
# Example documents
|
|
PUT index_1/my_type/1
|
|
{
|
|
"text": "Document in index 1"
|
|
}
|
|
|
|
PUT index_2/my_type/2?refresh=true
|
|
{
|
|
"text": "Document in index 2"
|
|
}
|
|
|
|
GET index_1,index_2/_search
|
|
{
|
|
"query": {
|
|
"terms": {
|
|
"_index": ["index_1", "index_2"] <1>
|
|
}
|
|
},
|
|
"aggs": {
|
|
"indices": {
|
|
"terms": {
|
|
"field": "_index", <2>
|
|
"size": 10
|
|
}
|
|
}
|
|
},
|
|
"sort": [
|
|
{
|
|
"_index": { <3>
|
|
"order": "asc"
|
|
}
|
|
}
|
|
],
|
|
"script_fields": {
|
|
"index_name": {
|
|
"script": {
|
|
"lang": "painless",
|
|
"source": "doc['_index']" <4>
|
|
}
|
|
}
|
|
}
|
|
}
|
|
--------------------------
|
|
// CONSOLE
|
|
|
|
<1> Querying on the `_index` field
|
|
<2> Aggregating on the `_index` field
|
|
<3> Sorting on the `_index` field
|
|
<4> Accessing the `_index` field in scripts
|