我需要一个带子查询的Elasticsearch查询

x4shl7ld  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(152)

我需要一个转换成弹性查询以下SQL的帮助
select * from index1where column1 not in(select column4 from index1)and column2= Value3.


应该只返回第三行
我试过使用脚本语言。但我是比较新的。我无法得到的结果

zynd9foi

zynd9foi1#

1.可以使用SQL search API
1.您可以使用the boolean query合并多个查询。

示例如下

POST test_sql/_bulk?refresh
{"index":{"_id":"1"}}
{"column1":"1","column2":"value3","column3":"value1","column4":""}
{"index":{"_id":"2"}}
{"column1":"2","column2":"value2","column3":"value2","column4":"1"}
{"index":{"_id":"3"}}
{"column1":"3","column2":"value3","column3":"value3","column4":""}

GET test_sql/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "column2": "value3"
          }
        }
      ],
      "must_not": [
        {
          "terms": {
            "column1": {
              "index": "test_sql",
              "path": "column4",
              "id": "2"
            }
          }
        }
      ]
    }
  }
}

这是Elasticsearch的搜索查询。它在“test_sql”索引中查找“column2”字段中的值为“value3”的文档。它还排除了“column1”字段中的值与ID为“2”的特定文档中的“column4”字段的值相匹配的文档。
检查column4字段值“_id”=2 =〉该值为1。排除值等于1的文档。“_id”=1排除,因为“column1”:“1”。

The more simple explanation for this query:
If a column4 value is equal to the column1 value exclude the document.

注意:在Elasticsearch中,terms查询不可能排除多个字段文档值。

相关问题