Inters Section Query in elastic search

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

"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.

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 combined in a bool query should work. Here's a short example of what I mean:

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

1 Like

Yes, This work.
Thank you :slight_smile:

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