0

I have an index with some documents having a field named "access_type" . It can have 2 values, either "faculty" or "students". For the documents with "faculty" as the value for "access_type", there will be another field called "faculties" which is a list of faculty name. So an example document would look like below:

{
  "access_type": "faculty",
   "faculties": [
      "facultyId1",
      "facultyId2",
      "facultyId3"
   ]
}

Now if we have two inputs say one is for the access_type and another is for the faculties. If I get the following input "faculty" and "facultyId4" . First I need to filter out all the documents matching the access type "faculty" and then in the resulting results the "facuultyId4" should search against the field "faculties". Since the "facultyId4" is not in the above document,it should not be considered a hit. How can I implement this as an elasticsearch query?

2 Answers 2

1

POST http://your.elastic.host:9200/index/type/_search

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "access_type": "faculty"
              }
            },
            {
              "term": {
                "faculties": "facultyId4"
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Hope this will for work.

GET index/type/_search
{
    "query": {
        "filtered": {
           "filter": {
               "and": {
                  "filters": [
                     {
                         "query": {
                             "match": {
                                "access_type": "faculty"
                             }
                         }
                     },
                     {
                         "query": {
                             "match": {
                                "faculties": "facultyId4"
                             }
                         }
                     }
                  ]
               }
           }
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.