Search code examples
sphinxthinking-sphinx

Is there way to create & index N no. of 'fields' dynamically with Sphinx


I am using Sphinx (with Thinking Sphinx v2.0 for RoR plugin),

Lets say I have several indexes on User model, lets say on 'name', 'address' and its one-to-many associations like 'posts' , 'comments' etc.

This means searching by post content would return me the User who made the post, and using :fieldmask 'rank mode' of sphinx, I am able to determine that the user was searched due to matching of 'posts'. But user has 'many' posts. So how to determine which 'post' it matched.

Is there any way, while indexing I can specify the index dynamically.? For e.g. If I can specify index 'post_1'='< post1content >' , 'post_5'='< post5content >' as different 'fields' for user1; similarly 'post_2', 'post_7' for user2, Thus after searching It would return me user2 matched with matching fields as post_7...


Solution

  • Sphinx can't have different fields for each record, I'm afraid, so what you're hoping to do isn't possible with that approach.

    If you need to know which posts match a query, I'd recommend conducting the search on the Post model instead, and then you can refer to a post's user? You could sort by user_id before weight, or group by user_id (so only one post per user is returned)? You'd be able to bring in user data into the Post index definition (and if a post has one user, then that data is kept to single values, instead of many, per record).

    Hope this gives you some clarity with your options.