ElasticSearch:如何使用OR条件查询数组字段?

5uzkadbs  于 2023-04-05  发布在  ElasticSearch
关注(0)|答案(3)|浏览(178)

我有几个ElasticSearch文档,其中包含一个Array字段。
我希望能够搜索整个索引
查找最先包含数组字段的文档,
如果是,则找到包含要搜索的值的那些文档。

以下是我的数据-

{
  "took" : 633,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon",
          "nick_name" : "an",
          "hobbies" : [
            "reading",
            "writing"
          ]
        }
      },
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon2",
          "nick_name" : "an2",
          "hobbies" : [
            "playing",
            "studying"
          ]
        }
      },
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon3",
          "nick_name" : "an3",
          "hobbies" : [
            "playing",
            "writing"
          ]
        }
      },
      {
        "_index" : "demo",
        "_type" : "1",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "anon4",
          "nick_name" : "an4"
        }
      }
    ]
  }
}

我想获取hobbies数组中包含**“玩”或“学”**值的文档
即文件2和3

以下是我的数据创建查询-

POST /demo/_doc/1
{
  "name":"anon",
  "nick_name":"an",
  "hobbies":["reading","writing"]
}

POST /demo/_doc/2
{
  "name":"anon2",
  "nick_name":"an2",
  "hobbies":["playing","studying"]
}

POST /demo/_doc/3
{
  "name":"anon3",
  "nick_name":"an3",
  "hobbies":["playing","writing"]
}

POST /demo/_doc/4
{
  "name":"anon4",
  "nick_name":"an4"
}

GET demo/_search
{
  "query": {
    "match_all": {}
  }
}

下面的查询可以让我进行单项搜索,但是如何实现Or条件呢?

GET demo/_search
{
  "query": {
    "match": {
      "hobbies": "playing"
    }
  }
}
41ik7eoe

41ik7eoe1#

我现在不能测试它,但它可能是这样的:

GET demo/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "hobbies": [
              "playing",
              "studying"
            ]
          }
        }
      ]
    }
  }
}

如果hobbies字段的类型为keyword

**编辑:**按照documentation中的示例,可能会更简单:

GET demo/_search
{
    "query" : {
        "terms" : {
            "hobbies" : ["playing", "studying"]
        }
    }
}
kyks70gy

kyks70gy2#

我能够使用以下es查询解决此问题-

GET demo/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "hobbies": "playing"
          }
        },
        {
          "match": {
            "hobbies": "studying"
          }
        }
      ]
    }
  }
}
uyhoqukh

uyhoqukh3#

这是另一个使用操作符的方法。

GET demo/_search
{
  "query": {
    "match": {
      "hobbies": {
        "query": "studying, playing",
        "operator": "OR"
      }
    }
  }
}

相关问题