0

Does there exist any query or filter that gives me only the documents where specific text field contains only a number value?

I have a text field (called "guess", feel free to use another name) that contain only one number in most cases ("guess":"333" for example, and not "guess":"34 34") or free text ("guess":"blah blah blah" for example, and "guess":"34 34" is free text too).

Best Regards!

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117

1 Answers1

3

Use a Regexp Filter (or Query)

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "regexp":{
                    "guess": "[0-9]*"
                }
            }
        }
    }
}

If you need to match more complex number formats, reference this post

Community
  • 1
  • 1
Peter Dixon-Moses
  • 3,169
  • 14
  • 18
  • If you have the luxury of discarding records with `guess` values that are not a single number, define a numeric [core type mapping property](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-core-types.html#number) for this field. (Or you can `ignore_malformed` so that the records with bad `guess` values are simply not searched) – Peter Dixon-Moses Aug 11 '15 at 18:03
  • i try by { "filtered": { "query": { "bool":{"must":[{"range":{"document.guess":{"from":"99999"}}},{"term":{"document.status":"active"}}]} }, "filter": { "regexp":{ "document.guess": "[0-9]*" } } } ,"from":0,"size":1,"sort":[],"facets":{} } – moralejaSinCuentoNiProverbio Aug 12 '15 at 20:16
  • but i get : an exception: { "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[yJYl31UrQ1qst3pCIfsZ_Q][mx][0]: RemoteTransportException[[Savage Steel][inet[/:9300]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[mx][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"filtered":{"query":{"bool":{"must":[{"range":{"document.guess":{"from":"99999"}}},{"term":{"document.status":"active"}}],"must_not":[],"should":[]}},"filter":{"regexp":{"document.guess":"[0-9]*"}}},"from":..... – moralejaSinCuentoNiProverbio Aug 12 '15 at 20:17
  • I only posted the inner part of the query DSL. Just updated the post to include the {"query": {...}} wrapper. – Peter Dixon-Moses Aug 13 '15 at 15:21
  • I will try it by java api I've lucky with java api, maybe i'm useless with jsons : ) – moralejaSinCuentoNiProverbio Aug 14 '15 at 15:43
  • Thanks for the help peterdm – moralejaSinCuentoNiProverbio Aug 14 '15 at 15:43