I'm indexing the following documents in Elasticsearch 5.x:
{
    "id": 1
    "description": "The quick brown fox"
}
{
    "id": 2
    "description": "The fox runs fast"
}
If I search for the word "fox" in these documents, I will get both of them.
 How can I find documents that their description field ends with the word "fox"? In the above example, I am looking the one with id=1
If possible, I prefer to do this with a Query String.
Well regex should work. Note if you have to look for endwith a lot of time, reindex using a analyzer will be the best (https://discuss.elastic.co/t/elasticsearch-ends-with-word-in-phrases/60473)
"regexp":{
            "id": {
                "value": "*fox",
            }
        }
Make sure your index mapping includes a keyword field for the description. For example:
PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "description": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "id": {
          "type": "long"
        }
      }
    }
  }
}
Add the documents:
POST my_index/_doc
{
    "id": 1,
    "description": "The quick brown fox"
}
POST my_index/_doc
{
    "id": 2,
    "description": "The fox runs fast"
}
And use a query string like this:
GET /my_index/_search
{
    "query": {
        "query_string" : {
            "default_field" : "description.keyword",
            "query" : "*fox"
        }
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With