node.jselasticsearchelasticsearch-querybooleanquery

ElasticSearch - Multi-match Query returning no hits for data even though its there - NodeJS


I'm trying to fetch a list of items that have the field: resellable: true.

Here is my data set I can see by visiting: my-domain.com/_all/listings/_search

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "collectibles",
        "_type" : "listing",
        "_id" : "SseZfNbpdpBxc3O",
        "_score" : 1.0,
        "_source" : {
          "data" : {
            "resellable" : true,
            "active" : true,
            "description" : "<p>1234 123 123 123</p>",
            "title" : "2134",
            "has_store" : true,
            "createdAt" : "Wed May 27 2020 04:23:18 GMT+0000 (Coordinated Universal Time)",
            "apiURL" : "kJ9zsIsdQG8TZrRfPco4",
            "id" : "SseZfNbpdpBxc3O",
            "state" : "PENDING",
            "amount" : "21",
          }
        }
      }
}

My query is using *NodeJS and ElasticSearch.js*

let results = await client.search({
    index: params.category,
    type: "listing",
    body: {
        query: {
            multi_match: {
                query: true,
                fields: ['resellable', 'active'],
                type: 'best_fields',
            }
        }
    }
})

The response is always no hits. I've tried a match, I've tried no best_fields and it doesn't seem to match the value.

What am I doing wrong here? Is there something else you need to do to query the _source.data level items?

ElasticSearch version: 7.4


Solution

  • You are using the "true" in string format when using multi_match, while you indexed data in boolean format true and thats the reason you are not getting any hits.

    I just noticed both of your multi_match fields(resellable and active) are boolean then why you are using the multi_match query, you should instead use the boolean filter query which is cached as well and gives better performance.

    From the doc

    Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.

    Sample index Mapping

    {
        "mappings": {
            "properties": {
                "active": {
                    "type": "boolean"
                },
                "resellable" :{
                    "type" : "boolean"
                }
            }
        }
    }
    

    Index various example documents

    {
       "active" : true,
       "resellable" : true
    }
    
    {
       "active" : true,
       "resellable" : false
    }
    
    {
       "active" : false,
       "resellable" : false
    }
    
    {
       "active" : false,
       "resellable" : true
    }
    

    Search query to get the doc where both values are true

    {
        "query": {
            "bool": {
                "filter": [
                    {
                        "match": {
                            "active": true
                        }
                    },
                    {
                        "match": {
                            "resellable": true
                        }
                    }
                ]
            }
        }
    }
    

    Result

     "hits": [
             {
                "_index": "filterbool",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.0,
                "_source": {
                   "active": true,
                   "resellable": true
                }
             }
          ]