1

I use the 7.6.1 version of Elasticsearch, I try to index simple objects and query them. I index with no problem, I'm able to query by id, but when I try to use search with Term query it fails (0 Hit). I don't understand what I'm doing wrong.

PUT product/_doc/b0264dad-6739-49ea-b691-e05a2883a724
{
  "name": "Name 2",
  "description": "Desc 1",
  "price": 11,
  "startSell": "2020-06-28T12:24:37.0070067+00:00",
  "endSell": "2020-07-19T12:24:37.0070161+00:00",
  "variants": [
    {
      "color": "Red",
      "size": 1
    },
    {
      "color": "Red",
      "size": 1
    },
    {
      "color": "Red",
      "size": 1
    },
    {
      "color": "Blue",
      "size": 1
    },
    {
      "color": "Yellow",
      "size": 1
    },
    {
      "color": "Yellow",
      "size": 2
    },
    {
      "color": "Yellow",
      "size": 3
    },
    {
      "color": "Purple",
      "size": 4
    }
  ]
}

Works and I can get the object by it's id :

GET product/_doc/b0264dad-6739-49ea-b691-e05a2883a724

Returns

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "b0264dad-6739-49ea-b691-e05a2883a724",
  "_version" : 1,
  "_seq_no" : 6,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Name 2",
    "description" : "Desc 1",
    "price" : 11,
    "startSell" : "2020-06-28T12:24:37.0070067+00:00",
    "endSell" : "2020-07-19T12:24:37.0070161+00:00",
    "variants" : [
      {
        "color" : "Red",
        "size" : 1
      },
      {
        "color" : "Red",
        "size" : 1
      },
      {
        "color" : "Red",
        "size" : 1
      },
      {
        "color" : "Blue",
        "size" : 1
      },
      {
        "color" : "Yellow",
        "size" : 1
      },
      {
        "color" : "Yellow",
        "size" : 2
      },
      {
        "color" : "Yellow",
        "size" : 3
      },
      {
        "color" : "Purple",
        "size" : 4
      }
    ]
  }
}

But all the three requests return no data, what am I doing wrong ? :

POST product/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "name": [
              "Name 2"
            ]
          }
        }
      ]
    }
  }
}


POST product/_search
{
  "query": {
    "term": {
      "name": {
        "value": "Name 2",
        "boost": 1
      }
    }
  }
}

POST product/_search
{
  "query": {
    "term": {
      "description": {
        "value": "Desc 1",
        "boost": 1
      }
    }
  }
}
Cladoo
  • 758
  • 6
  • 21

1 Answers1

2

Mostly its due to the fact that these fields name and description is defined as a text field in your index which uses the standard analyzer by default and split on the whitespace so tokens generated for these fields would be name and 2 for your name field and desc and 1 for description field.

while you are using the term query which is not analyzed and tried to match the name 2 token which doesn't exist.

Solution: Change the query to match query, otherwise use the .keyword field if mapping is generated automatically or define a keyword field to store the data if you intend to use the term query.

Refer difference b/w term vs match query with example

Amit
  • 30,756
  • 6
  • 57
  • 88
  • 1
    I ended up with the same conclusion, Kibana provides me the intellisense about the .keyword syntax, thank you for the useful link too! – Cladoo Jun 29 '20 at 14:43