简体   繁体   English

多值 object 在对象数组中时在 CrateDB 中搜索

[英]Multi-value object search in CrateDB when this one is within an array of objects

I'm trying to migrate our current ES to CrateDB and one of the issues I'm facing is searching for two specific values within the same object when this object is part of an array of objects.我正在尝试将我们当前的 ES 迁移到 CrateDB,我面临的一个问题是在同一个 object 中搜索两个特定值,而这个 object 是对象数组的一部分。

CREATE TABLE test.artefact (
      id INTEGER,
      metadata ARRAY(OBJECT(STATIC) AS (
          key_id INTEGER,
          value TEXT
      ))
);
insert into test.artefact(id, metadata) values (
  1,
  [
  {
    "key_id" = 1,
    "value" = 'TEST1'
  },
  {
    "key_id" = 2,
    "value" = 'TEST2'
  }
]
);

So basically, I'm trying to search metadata providing key_id and value.所以基本上,我正在尝试搜索提供 key_id 和值的元数据。

A select like this one finds artefact 1 as a match, even when key and value are in different objects:像这样的 select 会找到 artefact 1 作为匹配项,即使键和值在不同的对象中也是如此:

select * from test.artefact where 1 = ANY(metadata['key_id']) AND 'TEST2' = ANY(metadata['value'])

I have tried other functions, like UNNEST, with no luck.我尝试过其他功能,例如 UNNEST,但没有成功。

Copy from CrateDB Community: 从 CrateDB 社区复制:

One way that should work is一种应该起作用的方法是

SELECT *
FROM test.artefact
WHERE {key_id = 1, value = 'TEST2'} = ANY(metadata)

however this is probably not the most performant way.然而,这可能不是最高效的方式。

together with the queries on the fields it might be quick enough.加上对字段的查询,它可能足够快。

SELECT *
FROM test.artefact
WHERE
1 = ANY(metadata['key_id'])
AND 'TEST2' = ANY(metadata['value'])
AND {key_id = 1, value = 'TEST2'} = ANY(metadata)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM