英文:
Find a comparable value inside an array property within an Entity
问题
如何判断一个作为数组的属性是否包含某个值,在Xodus中,默认提供了txn.find(entityType, propertyName, comparableValue)
来查找值,然而,要找出数组属性是否包含某个值,最佳方法是什么呢?假设我有以下代码:
public class EmbeddedArrayIterable implements Serializable, ByteIterable {
}
我需要比较给定的值,例如"cat",是否在数组内。应该如何实现EmbeddedArrayIterable
,使其能够返回包含这个"cat"字符串的实体呢?
因此,我们可以进行如下操作:
// 这个属性是一个`EmbeddedArrayIterable`
// 它是一个类似于序列化JSON的数组,内容为["cat", "tiger", "lion"]
String propertyName = "keywords";
String comparableValue = "cat";
EntityIterable cats = txn.find(entityType, propertyName, comparableValue);
英文:
How do you find if a property that is an array contains a value, what is available by default to Xodus is txn.find(entityType, propertyName, comparableValue)
to find a value, however, what is the best way to find if an array property contains a value. Suppose I have this:
public class EmbeddedArrayIterable implements Serializable, ByteIterable {
}
In which I need to compare if a given value, example "cat", is inside the array. What should be the implementation of EmbeddedArrayIterable
as such it will be able to return the Entity
to which this "cat" string is in the array of the entity.
As such we can do:
// This property is a `EmbeddedArrayIterable`
// which is a serialized JSON like `["cat","tiger","lion"]`
String propertyName = "keywords";
String comparableValue = "cat";
EntityIterable cats = txn.find(entityType, propertyName, comparableValue);
答案1
得分: 1
如果您定义了自定义属性类型,那么您不能仅通过其值的一部分来查找实体。在您的情况下,使用开箱即用的 ComparableSet
值类型是有意义的。将属性值设置为一组项目:
final ComparableSet<String> propValue = new ComparableSet<>();
propValue.addItem("cat");
propValue.addItem("tiger");
propValue.addItem("lion");
entity.setProperty("keywords", propValue);
然后您可以使用 Entity#getProperty(..)
获取该值,将其转换为 ComparableSet<String>
并在需要时修改集合。您还可以仅通过集合中的单个项来查找实体:
EntityIterable cats = txn.find(entityType, "keywords", "cat");
英文:
If you define a custom property type, then you cannot find an entity by only a part of its value. In your case, it makes sense to use ComparableSet
value type provided out-of-the-box. Set the property value as a set of items:
final ComparableSet<String> propValue = new ComparableSet<>();
propValue.addItem("cat");
propValue.addItem("tiger");
propValue.addItem("lion");
entity.setProperty("keywords", propValue);
You can then get the value using Entity#getProperty(..)
, cast to ComparableSet<String>
and modify the set if necessary. You can also find the entity by only a single item of the set:
EntityIterable cats = txn.find(entityType, "keywords", "cat");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论