如何在同一个 Realm 查询中查询两个字段?
How to query two fields in same Realm query?
我需要根据名字和姓氏从 Realm Db 中查找客户。目前,我有这样的查询:
RealmResults<CustomerModel> results = realm
.where(CustomerModel.class)
.or()
.contains("firstname", input, Case.INSENSITIVE)
.or()
.contains("lastname", input, Case.INSENSITIVE)
.or()
.contains("addresses.street", input, Case.INSENSITIVE)
.or()
.contains("addresses.city", input, Case.INSENSITIVE)
.or()
.contains("addresses.postcode", input, Case.INSENSITIVE)
.findAllSorted("customerLocalId", Sort.DESCENDING);
这不能正常工作,因为我的名字和姓氏之间有 OR。
因此,如果我想查找名为 John Doe 的用户,它不会找到它,但如果我只键入 John,它会找到它。
我该如何解决这个问题?
为什么不分开白色 space?
String filter = input.trim().replaceAll("\s+", " ");
String[] tokens = filter.split(" ");
RealmQuery<CustomerModel> query = realm.where(CustomerModel.class);
for(int i = 0, size = tokens.length; i < size; i++) {
String token = tokens[i];
if(i != 0) {
query.or();
}
query.contains("firstname", token, Case.INSENSITIVE)
.or()
.contains("lastname", token, Case.INSENSITIVE)
.or()
.contains("addresses.street", token, Case.INSENSITIVE)
.or()
.contains("addresses.city", token, Case.INSENSITIVE)
.or()
.contains("addresses.postcode", token, Case.INSENSITIVE)
}
RealmResults<CustomerModel> results = query
.findAllSorted("customerLocalId", Sort.DESCENDING);
您需要使用群组:https://realm.io/docs/java/latest/#logical-operators
RealmResults<CustomerModel> results = realm
.where(CustomerModel.class)
.beginGroup()
.contains("firstname", input, Case.INSENSITIVE)
.contains("lastname", input, Case.INSENSITIVE)
.endGroup()
.or()
.contains("addresses.street", input, Case.INSENSITIVE)
.or()
.contains("addresses.city", input, Case.INSENSITIVE)
.or()
.contains("addresses.postcode", input, Case.INSENSITIVE)
.findAllSorted("customerLocalId", Sort.DESCENDING);
我需要根据名字和姓氏从 Realm Db 中查找客户。目前,我有这样的查询:
RealmResults<CustomerModel> results = realm
.where(CustomerModel.class)
.or()
.contains("firstname", input, Case.INSENSITIVE)
.or()
.contains("lastname", input, Case.INSENSITIVE)
.or()
.contains("addresses.street", input, Case.INSENSITIVE)
.or()
.contains("addresses.city", input, Case.INSENSITIVE)
.or()
.contains("addresses.postcode", input, Case.INSENSITIVE)
.findAllSorted("customerLocalId", Sort.DESCENDING);
这不能正常工作,因为我的名字和姓氏之间有 OR。
因此,如果我想查找名为 John Doe 的用户,它不会找到它,但如果我只键入 John,它会找到它。
我该如何解决这个问题?
为什么不分开白色 space?
String filter = input.trim().replaceAll("\s+", " ");
String[] tokens = filter.split(" ");
RealmQuery<CustomerModel> query = realm.where(CustomerModel.class);
for(int i = 0, size = tokens.length; i < size; i++) {
String token = tokens[i];
if(i != 0) {
query.or();
}
query.contains("firstname", token, Case.INSENSITIVE)
.or()
.contains("lastname", token, Case.INSENSITIVE)
.or()
.contains("addresses.street", token, Case.INSENSITIVE)
.or()
.contains("addresses.city", token, Case.INSENSITIVE)
.or()
.contains("addresses.postcode", token, Case.INSENSITIVE)
}
RealmResults<CustomerModel> results = query
.findAllSorted("customerLocalId", Sort.DESCENDING);
您需要使用群组:https://realm.io/docs/java/latest/#logical-operators
RealmResults<CustomerModel> results = realm
.where(CustomerModel.class)
.beginGroup()
.contains("firstname", input, Case.INSENSITIVE)
.contains("lastname", input, Case.INSENSITIVE)
.endGroup()
.or()
.contains("addresses.street", input, Case.INSENSITIVE)
.or()
.contains("addresses.city", input, Case.INSENSITIVE)
.or()
.contains("addresses.postcode", input, Case.INSENSITIVE)
.findAllSorted("customerLocalId", Sort.DESCENDING);