Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I query elasticsearch for only one type of record?

I am issuing a query to elasticsearch and I am getting multiple record types. How do I limit the results to one type?

like image 762
rjurney Avatar asked Mar 28 '12 21:03

rjurney


4 Answers

The following query will limit results to records with the type "your_type":

curl - XGET 'http://localhost:9200/_all/your_type/_search?q=your_query'

See http://www.elasticsearch.org/guide/reference/api/search/indices-types.html for more details.

like image 110
imotov Avatar answered Nov 04 '22 00:11

imotov


You can also use query dsl to filter out results for specific type like this:

$ curl -XGET 'http://localhost:9200/_search' -d '{
    "query": {
        "filtered" : {
            "filter" : {
                "type" : { "value" : "my_type" }
            }
        }
    }
}
'

Update for version 6.1: Type filter is now replaced by Type Query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html You can use that in both Query and Filter contexts.

like image 38
tikendra.chand Avatar answered Nov 04 '22 01:11

tikendra.chand


{
"query" : {
      "filtered" : { 
         "filter" : {
            "bool" : {
            "must" :[{"term":{"_type":"UserAudit"}}, {"term" : {"eventType": "REGISTRATION"}}]
           }
         }
      }
   },
   "aggs":{
      "monthly":{
         "date_histogram":{
            "field":"timestamp",
            "interval":"1y"
         },
         "aggs":{
            "existing_visitor":{
               "terms":{
                  "field":"existingGuest"
               }
            }
         }
      }
   }
}

"_type":"UserAudit" condition will look the records only specific to type

like image 42
Ankireddy Polu Avatar answered Nov 04 '22 01:11

Ankireddy Polu


On version 2.3 you can query _type field like:

{
  "query": {
    "terms": {
      "_type": [ "type_1", "type_2" ] 
    }
  }
}

Or if you want to exclude a type:

{
  "query": {
    "bool" : {
      "must_not" : {
        "term" : {
          "_type" : "Hassan"
        }
      }
    }
  }
}
like image 1
hpaknia Avatar answered Nov 03 '22 23:11

hpaknia