简体   繁体   中英

how to filter with script in elasticsearch

I need to search for something like this

POST dev_profiles/profiles/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "query_string": {
            "query": "user:asd"
          },
          "script_fields": {
            "message_age": {
              "script": {
                "source": "return doc.createdAt.value.getHour() == params.h",
                "params": {
                  "h": 9
                }
              }
            }
          }
        }
      ]
    }
  }
}

but can't build a query in the right way. Can somebody help?

In that case, it gives error [query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME] version of elasticsearch 6.8

The query you're looking for is like this:

{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "user": "asd"
          }
        }
        {
          "script": {
            "script": {
              "source": "return doc.createdAt.value.getHour() == params.h",
              "params": {
                "h": 9
              }
            }
          }
        }
      ]
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM