Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: adds facets to search #259

Merged
merged 11 commits into from
Jan 27, 2023
Merged

Feat: adds facets to search #259

merged 11 commits into from
Jan 27, 2023

Conversation

micheleriva
Copy link
Member

@micheleriva micheleriva commented Jan 24, 2023

This PR aims to add support for facets while searching with Lyra.
The API is pretty straightforward and will likely close #206, and pave the ground for #212.

Example API

Given the following database, we'll now need to select which fields will be available while performing a facet query:

const db = await create({
  schema: {
    id: "string",
    brand: "string",
    model: "string",
    year: "number",
    price: "number",
    used: "boolean",
    available: "boolean",
  }
});

Now let's insert some data. I'm experimenting with a format similar to the following one:

{"id":"3e4e0dc3-7cb9-4a03-9a36-2c66f51e08c9","brand":"Acura","year":2000,"model":"TL","available":true,"used":false,"price":36899},
{"id":"321b5c59-be55-48b9-a3d4-3e1586d1a863","brand":"Dodge","year":2009,"model":"Journey","available":false,"used":true,"price":12799},
{"id":"62123f1e-9f13-474a-a282-8d141c296edf","brand":"Nissan","year":1998,"model":"Frontier","available":true,"used":false,"price":48840},

So let's batch insert using the batchInsert built-in function:

await insertBatch(db, data);

Now we need to perform search. Lyra currently supports 3 data types: string, boolean, number, and we will be able to perform facet queries on all of them:

const results = await search(db, {
  term: "Dodge challenger",
  facets: {
    brand: {
      size: 10,
      sort: "ASC",
    },
    model: {
      size: 10,
      sort: "ASC",
    },
    available: {
      size: 10,
    },
    year: {
      ranges: [
        { from: 1990, to: 1995 },
        { from: 2000, to: 2005 },
        { from: 2005, to: 2010 },
        { from: 2010, to: 2015 },
        { from: 2015, to: 2020 },
      ]
    }
  },
  limit: 2,
})

This will lead to the following result:

{
  elapsed: 950750n,
  hits: [
    {
      id: 'a9c2a700-c23d-42d1-921d-99442d0d6af5',
      score: 8.586111395567093,
      document: {
        id: 'a9c2a700-c23d-42d1-921d-99442d0d6af5',
        brand: 'Mitsubishi',
        year: 1999,
        model: 'Challenger',
        available: false,
        used: true,
        price: 31861
      }
    },
    {
      id: '321b5c59-be55-48b9-a3d4-3e1586d1a863',
      score: 3.6680631599644267,
      document: {
        id: '321b5c59-be55-48b9-a3d4-3e1586d1a863',
        brand: 'Dodge',
        year: 2009,
        model: 'Journey',
        available: false,
        used: true,
        price: 12799
      }
    },
  ],
  count: 52,
  facets: {
    brand: { count: 2, values: { Dodge: 51, Mitsubishi: 1 } },
    model: {
      count: 26,
      values: {
        Viper: 4,
        Stratus: 4,
        'Ram 2500': 4,
        'Grand Caravan': 4,
        Dakota: 4,
        'Ram 3500': 4,
        'Dakota Club': 3,
        Journey: 2,
        Charger: 2,
        Nitro: 2
      }
    },
    available: { count: 2, values: { true: 27, false: 25 } },
    year: {
      count: 4,
      values: {
        '2000-2005': 15,
        '2005-2010': 13,
        '1990-1995': 10,
        '2010-2015': 5
      }
    }
  }
}

This is a work in progress

This PR is a huge work in progress. Types are not working at their best yet, and algorithms are not optimized at all. We should also return a list of IDs to perform a "group by" operation once a user decides to enter a facet.

Please do not review yet.

@micheleriva micheleriva mentioned this pull request Jan 24, 2023
@micheleriva micheleriva marked this pull request as ready for review January 27, 2023 09:59
@micheleriva micheleriva merged commit c23a938 into main Jan 27, 2023
@micheleriva micheleriva deleted the feat/facets branch January 27, 2023 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Facet Queries
1 participant