Elasticsearch-匹配所有arraylist字段

发布于 2025-01-29 04:42:38 字数 620 浏览 3 评论 0原文

我几乎没有数组“项目”的文档,我只想选择“ items.name”等于“红色”的那些文档。如果有一种带有一种红色和另一种颜色的文档,则不应产生结果。

1。

{
  "items": [
    {
      "id": "4",
      "name": "red"
    },
    {
      "id": "5",
      "name": "blue"
    }
  ]
}
{
  "items": [
    {
      "id": "3",
      "name": "red"
    }
  ]
}
{
  "items": [
    {
      "id": "2",
      "name": "red"
    },
    {
      "id": "1",
      "name": "red"
    }
  ]
}

现在,在这里,我需要一个查询,其中只能出现文件2和3的结果,因为所有“ items.name”中都存在“红色”。文档1被忽略,因为它也包含蓝色。

I have few documents with array "items" , i want to only pick those documents where "items.name" is equal to "red". If there is any document with one red and another color, then it should not come in result.

1.

{
  "items": [
    {
      "id": "4",
      "name": "red"
    },
    {
      "id": "5",
      "name": "blue"
    }
  ]
}
{
  "items": [
    {
      "id": "3",
      "name": "red"
    }
  ]
}
{
  "items": [
    {
      "id": "2",
      "name": "red"
    },
    {
      "id": "1",
      "name": "red"
    }
  ]
}

Now, here i need a query where only document 2 and 3 should come in result, as "red" is present in all the "items.name". document 1 is ignored as it contains blue also.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘味果▽酱 2025-02-05 04:42:38

这是我第一个带有脚本的解决方案:

GET test/_search
{
  "runtime_mappings": {
    "all_red_items": {
      "type": "boolean",
      "script": {
        "source": "int count = 0; for (int i = 0; i < doc['items.name'].size(); i++) { if (doc['items.name'][i] != 'red') { count++ }} emit(count == 0);"
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "all_red_items": {
              "value": true
            }
          }
        }
      ]
    }
  }
}

这是我的正则表达式解决方案:

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "items.name": {
              "value": "red"
            }
          }
        }
      ],
      "must_not": [
        {
          "regexp": {
            "items.name": "@&~(red)"
          }
        }
      ]
    }
  }
}

在发送请求之前,您需要准备下面的索引:

DELETE test

PUT test
{
  "mappings": {
    "properties": {
      "items": {
        "properties": {
          "name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}


POST test/_doc
{
  "items": [
    {
      "id": 6,
      "name": "red"
    },
    {
      "id": 5,
      "name": "blue"
    }
  ]
}

POST test/_doc
{
  "items": [
    {
      "id": 1,
      "name": "red"
    }
  ]
}


POST test/_doc
{
  "items": [
    {
      "id": 3,
      "name": "red"
    },
    {
      "id": 4,
      "name": "red"
    }
  ]
}

GET test/_mapping

Here is my first solution with the script :

GET test/_search
{
  "runtime_mappings": {
    "all_red_items": {
      "type": "boolean",
      "script": {
        "source": "int count = 0; for (int i = 0; i < doc['items.name'].size(); i++) { if (doc['items.name'][i] != 'red') { count++ }} emit(count == 0);"
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "all_red_items": {
              "value": true
            }
          }
        }
      ]
    }
  }
}

Here is my regular expression solution :

GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "items.name": {
              "value": "red"
            }
          }
        }
      ],
      "must_not": [
        {
          "regexp": {
            "items.name": "@&~(red)"
          }
        }
      ]
    }
  }
}

Before going to send the request, you need to prepare your index below :

DELETE test

PUT test
{
  "mappings": {
    "properties": {
      "items": {
        "properties": {
          "name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}


POST test/_doc
{
  "items": [
    {
      "id": 6,
      "name": "red"
    },
    {
      "id": 5,
      "name": "blue"
    }
  ]
}

POST test/_doc
{
  "items": [
    {
      "id": 1,
      "name": "red"
    }
  ]
}


POST test/_doc
{
  "items": [
    {
      "id": 3,
      "name": "red"
    },
    {
      "id": 4,
      "name": "red"
    }
  ]
}

GET test/_mapping
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文