1

In ElasticSearch stored documents, where in field "kitten" stored values of 2 types: words with numbers and only numbers. Example:

cat8389
fox973
947384

I want to get all documents, where in this array stored minimum one only numbers value, and can't write a correct query for it :(

I try this, but it's incorrect:

{
  "query": {
    "bool": {
      "must": [
        {
          "regexp": {
            "kitten": "[0-9 ]+$"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

Help me, please...

3
  • FYI: ES patterns are anchored by default, you do not have to/can't use $. The pattern must match the whole input. What do you mean by "minimum one only numbers value"? If you want to match digit-only fields,use "[0-9]+" Commented Mar 6, 2017 at 10:43
  • Thank you. This is correct query: { "query": { "bool": { "must": [ { "regexp": { "kitten": "[0-9]+" } } ], "must_not": [], "should": [] } }, "from": 0, "size": 10, "sort": [], "aggs": {} } Commented Mar 6, 2017 at 11:42
  • Posted, please consider accepting/upvoting. Commented Mar 6, 2017 at 11:51

1 Answer 1

2

ElasticSearch patterns are anchored by default, and you can't use the end of string $ anchor (same as start of string ^). That also means the pattern must match the whole input.

Most regular expression engines allow you to match any part of a string. If you want the regexp pattern to start at the beginning of the string or finish at the end of the string, then you have to anchor it specifically, using ^ to indicate the beginning or $ to indicate the end.

Lucene’s patterns are always anchored. The pattern provided must match the entire string.

So, to match digit-only inputs, use a mere "[0-9]+" pattern.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.