英文:
Isar: Is there a way to do a text search on the items of an IsarLinks object using indexes?
问题
现在,我将为您翻译您提供的内容:
我有一组`Bar`对象,每个对象都与`Foo`对象有isar链接:
@collection
class Bar{
Id? id;
final foos = IsarLinks<Foo>();
... // Bar的其他内容
};
每个`Foo`对象都有一个名为`text`的字段,其类型为`String`:
@collection
class Foo{
Id? id;
late String text;
... // Foo的其他内容
};
现在,我想要做的是搜索并找到与特定`Bar`对象关联的所有`Foo`对象,这些`Foo`对象在其`text`字段中包含所有给定的关键字(最好不区分大小写)。因此,使用关键字"canada"和"street"进行搜索应该匹配具有`text = "I don't know what street, Canada is on. - Al Capone"`的`Foo`对象。
Isar提供了一个[全文搜索的指南][1]。在其中,他们建议添加一个关于搜索文本中的单词的索引,如下所示:
@collection
class Foo{
Id? id;
late String text;
@Index(type: IndexType.value, caseSensitive: false) // 用于更多灵活性;请参阅 https://isar.dev/recipes/full_text_search.html#i-want-more-control
List<String> get words => Isar.splitWords(text); // 请参阅 https://isar.dev/recipes/full_text_search.html#splitting-text-the-right-way
};
然而,似乎没有一种简单的方法可以基于`IsarLinks`的索引进行过滤。
我的问题:
- 是否有一种方法可以通过`IsarLinks`对象的所有项进行基于索引的过滤?
- 如果没有,执行我所描述的文本搜索的最佳方法是什么?作为一个额外的问题,为什么不能使用`IsarLinks`来实现这个目标?
注意:我知道我可以简单地使用`IsarLinks.filter`并构建一个选择所有`text`包含所有关键字的`Foo`对象的查询,但我想知道是否有一种像`Isar`(带有索引)在`IsarLinks`上推荐的文本搜索方法。
[1]: https://isar.dev/recipes/full_text_search.html#full-text-search
英文:
I have a collection of Bar
s, each of which have isar links to Foo
s:
@collection
class Bar{
Id? id;
final foos = IsarLinks<Foo>();
... // Bar stuff
};
Each Foo
object has a field named text
, which is a String
:
@collection
class Foo{
Id? id;
late String text;
... // Foo stuff
};
Now, what I would like to do is search and find all the Foo
s linked to a particular Bar
that contain all of the given key words in their text
(preferably case insensitive). So, searching with the key words "canada" and "street" should match a Foo
with text = "I don't know what street, Canada is on. - Al Capone
Isar has a guide on how to do a full text search. In it, they recommend adding an index of the words of the text searched on, like so:
@collection
class Foo{
Id? id;
late String text;
@Index(type: IndexType.value, caseSensitive: false) // for more flexibility; see https://isar.dev/recipes/full_text_search.html#i-want-more-control
List<String> get words => Isar.splitWords(text); // see https://isar.dev/recipes/full_text_search.html#splitting-text-the-right-way
};
However, there does not seem to be an easy way to filer based on an index with IsarLinks
.
My questions:
- Is there a way to filter based on an index through all the items of an
IsarLinks
object? - If not, what would be the best way to perform a text search like I described? As a bonus question, why can't I do this with
IsarLinks
?
Note: I know I can simply use IsarLinks.filter
and build a query that selects all Foo
s with text
containing all the key words, but I am wondering if there is a way to do a text search in the way Isar
(with indexes) recommends on IsarLinks
.
答案1
得分: 1
Isar没有提供直接通过索引在IsarLinks
对象的所有项目上进行筛选的方法。索引功能主要设计用于对象的各个属性,而不是像IsarLinks
这样的复杂关系。
要根据text
字段执行对链接的Foo
对象的文本搜索,您可以按照以下步骤操作:
- 从特定
Bar
的IsarLinks
中检索链接的Foo
对象。 - 使用
where
方法和自定义条件基于搜索关键字筛选Foo
对象。 - 使用不区分大小写的搜索条件来匹配
text
字段中的关键字。
以下是文本搜索功能的示例实现:
import 'package:isar/isar.dart';
@Collection()
class Bar {
@Id()
int? id;
final foos = IsarLinks<Foo>();
// ... Bar stuff
}
@Collection()
class Foo {
@Id()
int? id;
late String text;
// ... Foo stuff
}
void searchTextInFoos(Bar bar, List<String> keywords) {
final foos = bar.foos;
final searchResults = foos
.where((foo) => keywords.every(
(keyword) => foo.text.toLowerCase().contains(keyword.toLowerCase()),
))
.toList();
// Use the searchResults as needed
}
在此示例中,searchTextInFoos
函数接受一个Bar
对象和一个搜索关键字的列表作为输入。它使用where
方法基于关键字筛选链接到该Bar
的Foo
对象。
where
方法对每个Foo
对象应用自定义条件,检查所有关键字是否包含在text
字段中。该条件使用不区分大小写的比较,将text
和keyword
都转换为小写。
最后,搜索结果存储在searchResults
列表中,您可以根据需要使用它们。
虽然通过索引在IsarLinks
对象上直接执行全文搜索将非常方便,但当前版本的Isar不提供此功能。但是,上述方法允许您实现所需的链接到Foo
对象的文本搜索功能。
英文:
Isar does not provide a direct way to filter based on an index through all the items of an IsarLinks
object. The indexing functionality is primarily designed for individual properties of an object rather than for complex relationships like IsarLinks
.
To perform a text search across the linked Foo
objects based on the text
field, you can follow these steps:
- Retrieve the linked
Foo
objects from theIsarLinks
of a specificBar
. - Filter the
Foo
objects based on the search keywords using thewhere
method and a custom condition. - Use a case-insensitive search condition to match the keywords in the
text
field.
Here's an example implementation of the text search functionality:
import 'package:isar/isar.dart';
@Collection()
class Bar {
@Id()
int? id;
final foos = IsarLinks<Foo>();
// ... Bar stuff
}
@Collection()
class Foo {
@Id()
int? id;
late String text;
// ... Foo stuff
}
void searchTextInFoos(Bar bar, List<String> keywords) {
final foos = bar.foos;
final searchResults = foos
.where((foo) => keywords.every(
(keyword) => foo.text.toLowerCase().contains(keyword.toLowerCase()),
))
.toList();
// Use the searchResults as needed
}
In this example, the searchTextInFoos
function takes a Bar
object and a list of search keywords
as input. It filters the Foo
objects linked to that Bar
based on the keywords using the where
method.
The where
method applies a custom condition to each Foo
object, checking if all keywords are contained within the text
field. The condition uses a case-insensitive comparison by converting both the text
and keyword
to lowercase.
Finally, the search results are stored in the searchResults
list, which you can utilize as needed.
While it would be convenient to perform a full-text search directly on an IsarLinks
object with the help of indexing, the current version of Isar does not provide this capability. However, the approach outlined above allows you to achieve the desired text search functionality for the linked Foo
objects.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论