nextapps-de / flexsearch

Next-Generation full text search library for Browser and Node.js
Apache License 2.0
12.54k stars 492 forks source link

Question about query with multiple words in multiple fields #314

Closed FrancescoBonizzi closed 2 years ago

FrancescoBonizzi commented 2 years ago

Consider this index definition:

const databaseIndex = new Document({
    preset: 'match',
    tokenize: 'full',
    language: 'it',
    resolution: 1,
    charset: 'latin:advanced',
    document: {
        id: 'id',
        index: ['id', 'brand', 'name'],
    },
});

I add the items to the document like this:

rawProductsList.forEach((element) => {
    databaseIndex.add(element);
});
Consider a simple set of objects like this: id brand name
0 oreo biscuit
1 barilla biscuit

When i search for 'oreo' or 'biscuit' I get the correct results, but what I want to achieve is that a query oreo biscuit gives me the object with id 1, but in my configuration it doesn't return anything.

How can I do it? Thanks.

ts-thomas commented 2 years ago

You will need to apply logical operator manually to the result (like union):

const index = new Document({
    preset: 'match',
    tokenize: 'full',
    language: 'it',
    resolution: 1,
    charset: 'latin:advanced',
    document: {
        id: 'id',
        index: ['id', 'brand', 'name'],
    },
});

index.add({ id: 0, brand: "oreo", name: 'biscuit' });
index.add({ id: 1, brand: "barilla", name: 'biscuit' });

const result = index.search([{
    field: "brand",
    query: "oreo"
},{
    field: "name",
    query: "biscuit"
}]);

const union = [...new Set(...result.map(res => res.result))];

console.log(union);
drzraf commented 6 months ago

While I understand the reason to leave this to the implementer, there are also strong ones to implement it internally:

Searching for results having two or more terms across all distinct Document fields is a must-have and should be possible without having to ressort to individual inefficient OR-ed queries.