Null_pointer_exception

Hello. I'm a newbie in elasticsearch and I have a strange elastic search behaviour. Could someone explain me why belowed query sometimes works and sometimes didnt work? This query should for current date found records. I should mention that in some docs date_from and date_to is equal null (it's mean that doc should be also returned) and I don't want to scoring will rely on on this fields. In result I should get random order of docs without scoring on these date fields (randomScore function has a random int as parameter). This query works for some dates and doesn't work for others.

{
  "from": 0,
  "size": 12,
  "_source": [   
    "city",     "latitude",    "description",    "title",       "id",    "lang",    "place_id",    "longitude",    "place_name",    "images",    "date_to",      "subcategory",        "date_from",    "www"
  ],
  "query": {
    "script_score": {
      "query": {
        "bool": {
          "must": {
            "bool": {
              "must": [
                {
                  "match": {
                    "lang": "pl"
                  }
                },
                {
                  "bool": {
                    "should": [
                      {
                        "bool": {
                          "must_not": {
                            "bool": {
                              "must": [
                                {
                                  "exists": {
                                    "field": "date_to"
                                  }
                                }
                              ]
                            }
                          }
                        }
                      },
                      {
                        "bool": {
                          "must": [
                            {
                              "range": {
                                "date_to": {
                                  "gte": "2020-02-10"
                                }
                              }
                            },
                            {
                              "range": {
                                "date_from": {
                                  "lte": "2020-02-10"
                                }
                              }
                            }
                          ]
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      },
      "script": {
        "source": "randomScore(4298)"
       
      }
    }
  }
}

Welcome!

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

Hi @pawelks.

i think your below query is working sometimes .
"must": {
"bool": {
"must": [
{
"match": {
"lang": "pl"
}
},

and rest all code is not taking any responses from your query data.

i think you are only getting output for matched with "lang": "pl"

not other than.

correct me if i am wrong here?

Thanks
HadoopHelp

Hi @rameshkr1994. Condition for dates works and ES returns docs that (date_from<=now and now<=date_to) or (date_to =null) - it was my intention. And when date is 2020-02-09 - it works and after whena date changes to 2020-02-10 it doesn't work. What's more, when I recently added new docs, dates that did not work before, began to work....

Ok @pawelks.

so problem here is you are bale to combine both matched for "lang":"pl" and "date":"Your date"

is that correct ?

if yes then you can't perform two must condition in single query.

you can also verify that by using separate must query.it will work ?but combining both in single it may not work?

please check and confirm me?i faced the issue while implemented same POC.as below-

may help you

Thanks
HadoopHelp

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.