3
votes

In Elasticsearch 5.6 using the following mapping:

"category" => [
    "type"=>"keyword",
    "doc_values"=>true,
    "index"=>false
    "store"=>true
]

I was given advice that it was possible to write a query that filters on this field because of its doc_values setting, even though the index attribute was set to false, but it seems like doc_values fields are only useful for aggregations and sorting.

Is it possible to create a query which filters on this field?

2
If the field is not indexed, it won't be searchable.Andrei Stefan
Thanks @AndreiStefan, if you can write this as an answer I'd be happy to award it to you.LaserJesus

2 Answers

4
votes

An field that is not indexed is, by definition, not searchable. Elasticsearch won't put it in the inverted index (which is used for searching). If you try to run a search query, you will get an error like Cannot search on field [category] since it is not indexed.

0
votes

Keyword datatypes have doc_values enabled by default. You don't need to enable it explicitly. And keyword fields can be filtered in query if the exact text is searched. For e.g., I have following index with mapping

PUT index_name
{
  "mappings": {
    "type_name": {
      "properties": {
        "id": {
          "type": "long"
        },
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}

Sample document:

{
    "id":1,
    "name":"hatim"
}

So, I can search by filtering it like this in query part

GET index_name/type_name/_search
{
  "query": {
    "term": {
      "name": "hatim"
    }
  }
}

And so on, you can add aggregation over it like this

GET index_name/type_name/_search
{
  "query": {
    "term": {
      "name": "hatim"
    }
  },
  "aggs": {
    "count": {
      "value_count": {
        "field": "name"
      }
    }
  }
}