Unable to do terms query

I am unable to a terms query on my data when I check the mapping field type was text. Is there a way where I can do an IN query on this field. There is only single data which is an id of my different database.

Welcome!

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

1 Like

Thank you for your instant reply. This is my mapping

"parsedIdentifiers": {
                    "properties": {
                        "imageUrl": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "productBvid": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }

And productBvid container the uuid of products. I want to add a terms query in it so that I can get only those products which ids I have provided. I am doing query like this

{
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "parsedIdentifiers.productBvid": ["4b1acb4f-8c6a-45c4-9b1d-676a338ce468"]
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "service_name": "starship"
                        }
                    },
                    {
                        "term": {
                            "audit": "true"
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "@timestamp": "desc"
            }
        ],
        "size": 2
    }

but I am unable to get any data. But when I run this query

{
        "query": {
            "bool": {
                "must": [
                    {
                        "match": {
                            "parsedIdentifiers.productBvid": "4b1acb4f-8c6a-45c4-9b1d-676a338cd468"
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "service_name": "starship"
                        }
                    },
                    {
                        "term": {
                            "audit": "true"
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "@timestamp": "desc"
            }
        ],
        "size": 2
    }

it works fine.

It's probably because of the type of the field or the analyzer used.
If you need more help regarding this, please provide a full script we can iterate on to help.

Also have a look at the analyze API. It's often helpful.

Elasticsearch is not allowing me to update the type. And the value which I am searching are like this 4b1acb4f-8c6a-45c4-9b1d-676a338ce468 what did you mean by analyzer? Token analyzer?

You need to reindex.

If you are using the default mapping, there's a chance that if you append .keyword to the field name, that will work.

Just a guess as I can't see your mapping.

I meant https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer-anatomy.html

1 Like

This is my mapping which is set as default

"parsedIdentifiers": {
                    "properties": {
                        "imageUrl": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "productBvid": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        }
                    }
                }

This is the result of Tokken Analyzer

{
  "tokens": [
    {
      "token": "e3bb4e66",
      "start_offset": 0,
      "end_offset": 8,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "014c",
      "start_offset": 9,
      "end_offset": 13,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "49e6",
      "start_offset": 14,
      "end_offset": 18,
      "type": "<ALPHANUM>",
      "position": 2
    },
    {
      "token": "b306",
      "start_offset": 19,
      "end_offset": 23,
      "type": "<ALPHANUM>",
      "position": 3
    },
    {
      "token": "c1c32fbfadc7",
      "start_offset": 24,
      "end_offset": 36,
      "type": "<ALPHANUM>",
      "position": 4
    }
  ]
}

So as I said, try:

{
        "query": {
            "bool": {
                "must": [
                    {
                        "terms": {
                            "parsedIdentifiers.productBvid.keyword": ["4b1acb4f-8c6a-45c4-9b1d-676a338ce468"]
                        }
                    }
                ],
                "filter": [
                    {
                        "term": {
                            "service_name": "starship"
                        }
                    },
                    {
                        "term": {
                            "audit": "true"
                        }
                    }
                ]
            }
        },
        "sort": [
            {
                "@timestamp": "desc"
            }
        ],
        "size": 2
    }

After trying I get following response

{
    "took": 1232,
    "timed_out": false,
    "_shards": {
        "total": 158,
        "successful": 158,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

My result is still not found

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

A script is something we can paste in Kibana and run.

Your solution worked. I mess up with the uuid that's why it wasn't working. Thank you :slightly_smiling_face: