how to write an Elasticsearch query having multiple conditions

不羁岁月 提交于 2021-02-05 09:44:39

问题


Need help constructing an ES query. Here's an example of the raw JSON of the documents:

{
  "user_uuid": 1234,
  "keywords": "apple",
  "@timestamp": "2020-01-15",
},
{
  "uuid": 1234,
  "keywords": "google",
  "@timestamp": "2020-01-21",
},
{
  "uuid": 9876,
  "keywords": "youtube",
  "@timestamp": "2020-01-25",
}

Here is an example requirement:

{
    "search_groups":[
        {
            "keywords": [
                "google",
                "microsoft",
                "tesla"
            ],
            "time_range": 2020-01-17 - 2020-01-22
        },
        {
            "keywords": [
                "apple",
                "youtube",
                "spotify"
            ],
            "time_range": 2020-01-10 - 2020-01-25
        },

    ]
}

Here is what I want to achieve based on the above requirement:

I should be able to get the unique user_uuid which satisfies all the search groups. That is,

Get unique uuids where both conditions are true:
  (
    (keywords should match one of "google" OR "microsoft" OR "tesla")
    and
    (@timestamp must be between "2020-01-17" and "2020-01-22")
  )
  AND
  (
    (keywords should match one of "apple" OR "youtube" OR "spotify")
    and
    (@timestamp must be between "2020-01-10" and "2020-01-25")
  )

1234 is the user_uuid which satisfies this query.

How do I construct this as an ES query?

来源:https://stackoverflow.com/questions/59949450/how-to-write-an-elasticsearch-query-having-multiple-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!