0

This is example of field in ES document. How can i query 'prices.value' that can be only integers?

In this case, value is "150.99" and can fully be converted to integer. But sometimes in can be a text something like "value": "a lot" and i want to exclude document with this values.

 "prices": [
     {
      "currency": "RUR",
      "id_offer": 605994811,
       "id_prcdoc": 42172,
        "id_prcknd": 20859,
         "info": {},
         "min_order": null,
         "sell_by": null,
         "value": "150.99"}]

Index of this field:

"prices": {
        "type": "nested",
        "properties": {
          "currency": {
            "type": "keyword"
          },
          "id_offer": {
            "type": "integer"
          },
          "id_prcdoc": {
            "type": "integer"
          },
          "id_prcknd": {
            "type": "integer"
          },
          "min_order": {
            "type": "keyword"
          },
          "sell_by": {
            "type": "keyword"
          },
          "value": {
            "type": "keyword",
            "index": false
          }
        }
      }

And sometimes it 'value' field can be '0.00' and probably i want to exclude this values to..

1 Answer 1

1

You can use painless script to check if value can be converted to number.

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "prices",
            "query": {
              "script": {
                "script": "if(doc['prices.value'].size()==0){return false;}if(doc['prices.value'].value=='0.00'){return false;}try{ Double.parseDouble(doc['prices.value'].value); return true;} catch(Exception e){return false;} "
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

Result will be in inner_hit. Since scripts are slow it is better to resolve it while indexing . Another field can we created which will have value only if it is price value is number, and in query this field can be used

EDIT:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "prices",
            "query": {
              "bool": {
                "must": [
                  {
                    "regexp": {
                      "prices.value": "[1-9][0-9]*.*[0-9]*"
                    }
                  }
                ]
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is kinda expensive, but anyway, thank you, i'll experiment with it)
i have added an edit to use regex m will be better performant than script

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.