# Search based on ID values in nested queries

**URL:** <https://discuss.elastic.co/t/search-based-on-id-values-in-nested-queries/182304>\
**Category:** Elasticsearch\
**Created:** [May 22, 2019, 6:48pm UTC](https://discuss.elastic.co/t/search-based-on-id-values-in-nested-queries/182304 "2019-05-22T18:48:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![waynesmallman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/waynesmallman/32/18596_2.png) [@waynesmallman](https://discuss.elastic.co/u/waynesmallman)\
**Post date:** [May 22, 2019, 6:48pm UTC](https://discuss.elastic.co/t/search-based-on-id-values-in-nested-queries/182304/1 "2019-05-22T18:48:27Z")

</div>

Hi!

I have _mapping_ that contains a `title` and `note` field, in addition to a `links_to_asset` field:

```
"links_to_asset": {
	"type": "nested",
	"properties": {
		"comment": {
			"type": "text",
			"fields": {
				"std": {
					"type": "text",
					"fields": {
						"std": {
							"type": "text",
							"analyzer": "standard"
						}
					},
					"analyzer": "asset_en_analyzer"
				}
			}
		},
		"creation": {
			"type": "date",
			"format": "date_hour_minute_second"
		},
		"from_asset": {
			"type": "integer"
		},
		"modification": {
			"type": "date",
			"format": "date_hour_minute_second"
		},
		"note_link_id": {
			"type": "long"
		},
		"to_asset": {
			"type": "integer"
		},
		"user_id": {
			"type": "long"
		}
	}
}

```

... where the `from_asset` and `to_asset` fields are equal to the `_id` field values in documents.

So far, I have:

```
{
    "query": {
        "nested": {
            "path": "links_to_asset",
            "query": {
                "bool": {
                    "must": {
                        "match": {
                            "links_to_asset.comment": "research suggests chaotic"
                        }
                    }
                }
            }
        }
    }
}

```

... which is working, but I'm struggling with a few things:

1. is it possible to restrict the search to documents by their `_id` and `user_id` fields;
2. and is it possible to search the `title` fields of the documents belonging to the `from_asset` and `to_asset` fields?

I've been experimenting but with no success (I'm unable to combine `function_score` with `nested`, as an example).

---

<div class="post-metadata">

**Author:** ![waynesmallman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/waynesmallman/32/18596_2.png) [@waynesmallman](https://discuss.elastic.co/u/waynesmallman)\
**Post date:** [May 24, 2019, 2:42pm UTC](https://discuss.elastic.co/t/search-based-on-id-values-in-nested-queries/182304/2 "2019-05-24T14:42:37Z")

</div>

Based on an answer to [a question on StackOverflow](https://stackoverflow.com/a/45623105/630530), I've had a partial success with:

```
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "user_id": 11
                    }
                },
                {
                    "term": {
                        "_id": 19976
                    }
                },
                {
                    "nested": {
                        "path": "links_to_asset",
                        "score_mode": "max",
                        "query": {
                            "bool": {
                                "must": {
                                    "match": {
                                        "links_to_asset.comment": "complex pattern resembles wave scientist"
                                    }
                                }
                            }
                        },
                        "inner_hits": {}
                    }
                }
            ]
        }
    }
}

```

... however, I still need to — somehow — use the `_id` value to retrieve the title from the document to which is relates.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 21, 2019, 2:42pm UTC](https://discuss.elastic.co/t/search-based-on-id-values-in-nested-queries/182304/3 "2019-06-21T14:42:51Z")

</div>

Closed on June 21, 2019, 2:42pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
