Skip to content

Commit

Permalink
Correct formatting for retrieve specific fields (#9057)
Browse files Browse the repository at this point in the history
Signed-off-by: Fanit Kolchina <[email protected]>
  • Loading branch information
kolchfa-aws authored Jan 13, 2025
1 parent 20047e6 commit 83afb9b
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions _search-plugins/searching-data/retrieve-specific-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ If `_source` is disabled in the index mappings, [searching with docvalue fields]
You can list the fields you want to retrieve in the `fields` parameter. Wildcard patterns are also accepted:

```json
GET "/index1/_search?pretty"
GET /index1/_search
{
"_source": false,
"fields": ["age", "nam*"],
Expand Down Expand Up @@ -169,7 +169,7 @@ The following example demonstrates how to use the `docvalue_fields` parameter.
1. Create an index with the following mappings:

```json
PUT my_index
PUT /my_index
{
"mappings": {
"properties": {
Expand All @@ -186,15 +186,18 @@ The following example demonstrates how to use the `docvalue_fields` parameter.
2. Index the following documents into the newly created index:

```json
POST my_index/_doc/1
POST /my_index/_doc/1
{
"title": "OpenSearch Basics",
"author": "John Doe",
"publication_date": "2021-01-01",
"price": 29.99
}
```
{% include copy-curl.html %}

POST my_index/_doc/2
```json
POST /my_index/_doc/2
{
"title": "Advanced OpenSearch",
"author": "Jane Smith",
Expand All @@ -207,7 +210,7 @@ The following example demonstrates how to use the `docvalue_fields` parameter.
3. Retrieve only the `author` and `publication_date` fields using `docvalue_fields`:

```json
POST my_index/_search
POST /my_index/_search
{
"_source": false,
"docvalue_fields": ["author", "publication_date"],
Expand Down Expand Up @@ -259,7 +262,7 @@ In OpenSearch, if you want to retrieve doc values for nested objects, you cannot
1. Define the index mappings:

```json
PUT my_index
PUT /my_index
{
"mappings": {
"properties": {
Expand All @@ -282,7 +285,7 @@ In OpenSearch, if you want to retrieve doc values for nested objects, you cannot
2. Index your data:

```json
POST my_index/_doc/1
POST /my_index/_doc/1
{
"title": "OpenSearch Basics",
"author": "John Doe",
Expand All @@ -305,7 +308,7 @@ In OpenSearch, if you want to retrieve doc values for nested objects, you cannot
3. Perform a search with `inner_hits` and `docvalue_fields`:

```json
POST my_index/_search
POST /my_index/_search
{
"query": {
"nested": {
Expand Down Expand Up @@ -405,7 +408,7 @@ Unlike `_source`, `stored_fields` must be explicitly defined in the mappings for
1. Create an index with the following mappings:

```json
PUT my_index
PUT /my_index
{
"mappings": {
"properties": {
Expand All @@ -432,14 +435,17 @@ Unlike `_source`, `stored_fields` must be explicitly defined in the mappings for
2. Index your data:

```json
POST my_index/_doc/1
POST /my_index/_doc/1
{
"title": "OpenSearch Basics",
"author": "John Doe",
"publication_date": "2022-01-01",
"price": 29.99
}
```
{% include copy-curl.html %}

```json
POST my_index/_doc/2
{
"title": "Advanced OpenSearch",
Expand All @@ -453,7 +459,7 @@ Unlike `_source`, `stored_fields` must be explicitly defined in the mappings for
3. Perform a search with `stored_fields`:

```json
POST my_index/_search
POST /my_index/_search
{
"_source": false,
"stored_fields": ["title", "author"],
Expand Down Expand Up @@ -508,7 +514,7 @@ In OpenSearch, if you want to retrieve `stored_fields` for nested objects, you c
1. Create an index with the following mappings:

```json
PUT my_index
PUT /my_index
{
"mappings": {
"properties": {
Expand All @@ -531,7 +537,7 @@ In OpenSearch, if you want to retrieve `stored_fields` for nested objects, you c
2. Index your data:

```json
POST my_index/_doc/1
POST /my_index/_doc/1
{
"title": "OpenSearch Basics",
"author": "John Doe",
Expand All @@ -554,7 +560,7 @@ In OpenSearch, if you want to retrieve `stored_fields` for nested objects, you c
3. Perform a search with `inner_hits` and `stored_fields`:

```json
POST my_index/_search
POST /my_index/_search
{
"_source": false,
"query": {
Expand Down Expand Up @@ -641,7 +647,7 @@ You can include or exclude specific fields from the `_source` field in the searc
1. Index your data:

```json
PUT my_index/_doc/1
PUT /my_index/_doc/1
{
"title": "OpenSearch Basics",
"author": "John Doe",
Expand All @@ -654,7 +660,7 @@ You can include or exclude specific fields from the `_source` field in the searc
2. Perform a search using source filtering:

```json
POST my_index/_search
POST /my_index/_search
{
"_source": ["title", "author"],
"query": {
Expand Down Expand Up @@ -694,7 +700,7 @@ The following is the expected response:
You can choose to exclude fields by using the `"excludes"` parameter in a search request, as shown in the following example:

```json
POST my_index/_search
POST /my_index/_search
{
"_source": {
"excludes": ["price"]
Expand Down Expand Up @@ -854,26 +860,26 @@ If you have an index of products, where each product document contains the `pric

2. Use the `script_fields` parameter to include a custom field called `discounted_price` in the search results. This field will be calculated based on the `price` and `discount_percentage` fields using a script:

```json
GET /products/_search
{
"_source": ["product_id", "name", "price", "discount_percentage"],
"query": {
"match": {
"category": "Electronics"
}
},
"script_fields": {
"discounted_price": {
"script": {
"lang": "painless",
"source": "doc[\"price\"].value * (1 - doc[\"discount_percentage\"].value / 100)"
```json
GET /products/_search
{
"_source": ["product_id", "name", "price", "discount_percentage"],
"query": {
"match": {
"category": "Electronics"
}
},
"script_fields": {
"discounted_price": {
"script": {
"lang": "painless",
"source": "doc[\"price\"].value * (1 - doc[\"discount_percentage\"].value / 100)"
}
}
}
}
}
}
```
{% include copy-curl.html %}
```
{% include copy-curl.html %}

You should receive the following response:

Expand Down

0 comments on commit 83afb9b

Please sign in to comment.