How to filter the following query?

Hi,
I am using the following search:

{
    "_source": [
        "title",
        "bench",
        "court",
        "id_"
    ],
    "size": 10,
    "query": {
    	
        "bool": {
            "must": {
                
                    "multi_match": {
                    	"query": "murder" 
                    ,
                    "fields": [
                        "title",
                        "content"
                    ]
                }
            },
            
            "should": {
                "multi_match": {
                    "query": "murder",
                    "fields": [
                        "title.standard",
                        "content.standard"
                    ]
                }
            }
            
        }
          
    
    },
    "highlight": {
        "fields": {
            "title": {},
            "content": {}
        }
    }
}

I now want to filter the results using the id (_id) elastic search gave it during indexing. For example, {"_id" : 5903}. I guess you have to use the term query. The results should be such that only if the _id is matched, the document returns. How can I do that?

Term query will work just same as it works for other fields. You can even use ids query. Just add to the must clause as below:

{
  "_source": [
    "title",
    "bench",
    "court",
    "id_"
  ],
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "murder",
            "fields": [
              "title",
              "content"
            ]
          }
        },
        {
          "ids":{
            "values": [5903]
          }
        }
      ],
      "should": {
        "multi_match": {
          "query": "murder",
          "fields": [
            "title.standard",
            "content.standard"
          ]
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "title": {},
      "content": {}
    }
  }
}

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