bool option in search
I think this part of documentation is wrong
There is one situation where the bool property is still supported. When you like to switch the default "or" logic from the field search into "and", e.g.:
index.search(query, {
index: ["title", "content"],
bool: "and"
});
You will just get results which contains the query in both fields. That's it.
This doesn't work:
import flexsearch from 'flexsearch';
const { Document } = flexsearch;
const index = new Document({
tokenize: 'strict',
document: {
id: 'id',
index: [
{
field: 'text'
},
{
field: 'title'
}
]
}
});
index.add({
id: 0,
text: 'some text abc abc',
title: 'tag1 tag2 tag3'
});
index.add({
id: 1,
text: 'banana1 ulala abc',
title: 'tag4 tag5 abc'
});
console.log(index.search('abc', { bool: 'or' }));
returns:
[
{ field: 'text', result: [ 0, 1 ] },
{ field: 'title', result: [ 1 ] }
]
and
console.log(index.search('abc', { bool: 'and' }));
also returns the same thing.
I would expect only 1 to be returned somehow because abc appears only in document 1 in both text and tags.
I then went checking in the Document.search function and it appears to only consider and bool option if tags are present as well.
I did more exploring... so that part of documentation mentioned above is indeed wrong...
there is similar passage in Tags section:
This gives you result which are tagged with one of the given tag.
Multiple tags will apply as the boolean "or" by default. It just needs one of the tags to be existing.
This is another situation where the bool property is still supported. When you like to switch the default "or" logic from the tag search into "and", e.g.:
index.search(query, {
index: "content",
tag: ["dog", "animal"],
bool: "and"
});
You will just get results which contains both tags (in this example there is just one records which has the tag "dog" and "animal").
Which works:
const index = new Document({
document: {
id: 'id',
tag: 'tags',
index: 'text'
}
});
index.add({
id: 0,
text: 'some text',
tags: ['tag1', 'tag2', 'tag3']
});
index.add({
id: 1,
text: 'some thing',
tags: ['tag1', 'tag5']
});
console.log(index.search('some', { bool: 'and', tag: ['tag1', 'tag2'] }));
This does return only document 0 when and is used and both document 0 and 1 when or option is used.
or
[ { field: 'text', result: [ 0, 1 ] } ]
and
[ { field: 'text', result: [ 0 ] } ]
Another small caveat is that index.search({ bool: 'and', tag: ['tag1', 'tag2'] }) (so search without query) ignores bool option again. This behaviour is implemented here.
But it might actually be logical because in this case results look different anyway:
[ { tag: 'tag1', result: [ 0, 1 ] }, { tag: 'tag2', result: [ 0 ] } ]
but it's good to be aware and perhaps mention in docs as well.. that query has to be present for bool option to work (in tags) and that doesn't work anywhere else.