【问题标题】:Filter document on items in an array ElasticSearch using Where In使用 Where In 过滤数组 ElasticSearch 中项目的文档
【发布时间】:2021-02-17 04:54:53
【问题描述】:

我有数据:

[
  {
    "NAME": "John Doe", 
    "CLASS":[1,10,30]
  },
  {
    "NAME": "Albert",
    "CLASS": [1,20,40]
  },
  {
    "NAME": "XINN",
    "CLASS": [10,30]
  },
  {
    "NAME": "UJANG",
    "CLASS": [20,40]
  },
  {
    "NAME": "BAMBANG",
    "CLASS": [30,40]
  }
]

我有以下查询 SQL:

SELECT * FROM table_name WHERE CLASS IN (1,20)

我想要的是:

[{"NAME": "John Doe","CLASS":[1,10,30]},{"NAME": "Albert","CLASS": [1,20,40]},{"NAME": "UJANG","CLASS": [20,40]}]

如何更改搜索以匹配 SQL 查询?

【问题讨论】:

  • 与我们分享你到目前为止所做的事情

标签: javascript elasticsearch


【解决方案1】:

您可以使用terms query 来获得您的预期结果,并添加一个工作示例。

索引示例文档

{
    "name" : "John Doe",
    "class" : [1, 10,30]
}

{
    "name": "Albert",
    "class": [1,20,40]
}

{
    "name": "XINN",
    "class": [10,30]
}

{
    "name": "UJANG",
    "class": [20,40]
}

{
    "name": "BAMBANG",
    "class": [30,40]
}

搜索查询

{
    "query": {
        "terms": {
            "class": [
                1,20
            ]
        }
    }
}

以及搜索结果

"hits": [
            {
                "_index": "shingleside",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "name": "John Doe",
                    "class": [
                        1,
                        10,
                        30
                    ]
                }
            },
            {
                "_index": "shingleside",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "name": "Albert",
                    "class": [
                        1,
                        20,
                        40
                    ]
                }
            },
            {
                "_index": "shingleside",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "name": "UJANG",
                    "class": [
                        20,
                        40
                    ]
                }
            }
        ]

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多