Search code examples
yapdatabase

Multiple index-entries for a single Yapdatabase-row


I´m using Yapdatabase as storage engine in my iOS-application, and I need an index representing multiple values of the same property of an object.

Example: I´m storing car objects in my Yapdatabase. Most of my car objects have multiple colors, but I´d like to efficiently retrieve all yellow cars.

I´m somewhat familiar with YapDatabaseSecondaryIndexes, but I´ve not been able to fit that into my scenario.

How may I efficiently retrieve yellow cars?


Solution

  • There are a couple options. If you're only dealing with a handful of colors, then you can still use YapDatabaseSecondaryIndex. You could configure your index to include a field for each color.

    YapDatabaseSecondaryIndexSetup *setup = [[YapDatabaseSecondaryIndexSetup alloc] init];
    [setup addColumn:@"red" withType:YapDatabaseSecondaryIndexTypeInteger];
    [setup addColumn:@"blue" withType:YapDatabaseSecondaryIndexTypeInteger];
    ...
    

    And then each car simply sets the "flags" for each color.

    If you're dealing with a lot of different colors, then it may be more effective to use the FullTextSearch extension instead. Basically, you're going to create a string for each car that contains all the colors, and hand that string to the FullTextSearch extension. And then you can simply issue queries for cars that contain "yellow".