# Inters Section Query in elastic search

**URL:** <https://discuss.elastic.co/t/inters-section-query-in-elastic-search/235777>\
**Category:** Elasticsearch\
**Created:** [June 4, 2020, 2:12pm UTC](https://discuss.elastic.co/t/inters-section-query-in-elastic-search/235777 "2020-06-04T14:12:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abba5](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@abba5](https://discuss.elastic.co/u/abba5)\
**Post date:** [June 4, 2020, 2:12pm UTC](https://discuss.elastic.co/t/inters-section-query-in-elastic-search/235777/1 "2020-06-04T14:12:12Z")

</div>

I am working on a project where I need to search for a person's name and it's college.  
Indexing:

```auto
"attributes": {
	"type": "nested",
	"properties": {
		"name": {
			"type": "keyword"
		},
		"value": {
			"type": "text",
			"analyzer": "whitespace",
			"fields": {
				"noncase": {
					"type": "text",
					"analyzer": "standard"
				},
				"synonym": {
					"type": "text",
					"analyzer": "synonym"
				},
				"whitespace": {
					"type": "text",
					"analyzer": "whitespace"
				},
				"phonetic": {
					"type": "text",
					"analyzer": "dbl_metaphone"
				}
			}
		}
	}
}

```

here I will store the value of a person as  
attributes.name = "First\_Name",  
attributes.value = "John"

attributes.name = "College\_Name",  
attributes.value = "XYZ"

Is there a way to search where both intersection people come into the result.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [June 5, 2020, 10:49am UTC](https://discuss.elastic.co/t/inters-section-query-in-elastic-search/235777/2 "2020-06-05T10:49:21Z")

</div>

Hi @abba5,

I assume your "attributes" are nested inside a main document and you want to find the documents that contain a certain person that attended a certain college. I think using two [nested queries](https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-nested-query.html) combined in a `bool` query should work. Here's a short example of what I mean:

```auto
DELETE /test

PUT /test
{
  "mappings": {
    "properties": {
      "maindocid": {
        "type": "keyword"
      },
      "attributes": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "keyword"
          },
          "value": {
            "type": "text",
            "analyzer": "standard"
          }
        }
      }
    }
  }
}

POST /test/_bulk?refresh=wait_for
{ "index": { "_id": "1" } }
{ "maindocid": "1", "attributes" : [{ "name" : "first_name", "value" : "John"}, { "name" : "college", "value" : "xyz"}] }
{ "index": { "_id": "2" } }
{ "maindocid": "2", "attributes" : [{ "name" : "first_name", "value" : "John"}, { "name" : "college", "value" : "othercollege"}] }
{ "index": { "_id": "3" } }
{ "maindocid": "3", "attributes" : [{ "name" : "first_name", "value" : "Jane"}, { "name" : "college", "value" : "xyz"}] }

GET /test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "attributes",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "attributes.name": "first_name"
                    }
                  },
                  {
                    "match": {
                      "attributes.value": "John"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "attributes",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "attributes.name": "college"
                    }
                  },
                  {
                    "match": {
                      "attributes.value": "xyz"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

Does this help?

Cheers

---

<div class="post-metadata">

**Author:** ![abba5](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@abba5](https://discuss.elastic.co/u/abba5)\
**Post date:** [June 5, 2020, 11:37am UTC](https://discuss.elastic.co/t/inters-section-query-in-elastic-search/235777/3 "2020-06-05T11:37:41Z")

</div>

Yes, This work.  
Thank you 🙂

---

<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:** [July 3, 2020, 11:37am UTC](https://discuss.elastic.co/t/inters-section-query-in-elastic-search/235777/4 "2020-07-03T11:37:47Z")

</div>

Closed on July 3, 2020, 11:37am UTC

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