Search code examples
ruby-on-railselasticsearchruby-on-rails-5filteringelasticsearch-6

Elastic search 6.3 term filter not working on integer columns


I have integrated elasticsearch in a ROR application.

I am using elastic search to find products named 'xyz' and filtered on basis of star count = 1

This is my mapping:

    {
      :product => {
        :dynamic => false,

        :properties => {
          :name => {
            :analyzer => "english",
            :type => "text"
          },
          :description => {
            :analyzer => "english",
            :type => "text"
          },
          :available => {
            :type => :boolean
          },
          :last_updated => {
            :type => :date
          },
          :categories_names => {
            :type => "text"
          },
          :stars => {
            :type => :integer
          }
        }
      }
    }

I am using the following query:

    {

      "query": {

        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "xyz",
                "fields": ["name", "description"],
                "type": "most_fields"
              }
            },
            {
              "match": {
                "available": true
              }
            }
          ],
          "filter": {
            "term": {
              "stars": 1
            }
          }
        }
      }
    }

This gives me no match but I have product records with stars having value 1. Also, range query is not giving any results with field stars.

This is the record that should match the above query:

#<Product> {
                         "id" => 90,
                       "name" => "xyz",
               "last_updated" => Tue, 22 May 2018 15:04:43 UTC +00:00,
                      "stars" => 1,
                "description" => "training",
                  "available" => true,
                 "created_at" => Thu, 21 Jun 2018 11:56:15 UTC +00:00,
                 "updated_at" => Fri, 29 Jun 2018 10:30:06 UTC +00:00,
}

Solution

  • try adding doc_value: true while indexing the field

    indexes :stars, type: 'integer', doc_values: true

    Most fields are indexed by default, which makes them searchable. The inverted index allows queries to look up the search term in unique sorted list of terms, and from that immediately have access to the list of documents that contain the term.

    Sorting, aggregations, and access to field values in scripts require a different data access pattern. Instead of looking up the term and finding documents, we need to be able to look up the document and find the terms that it has in a field.

    Doc values are the on-disk data structure, built at document index time, which makes this data access pattern possible. They store the same values as the _source but in a column-oriented fashion that is way more efficient for sorting and aggregations. Doc values are supported on almost all field types, with the notable exception of analyzed string fields.

    Though default doc_values is true but explicitly adding it solved my purpose