Search code examples
javasolrsolrj

Indexing Pairs <key, value> and return results ordered by value on Solr


I have a collection of users. Each user has some fields. One of those fields is a map, like: "key, value" in witch the value represents the order.

For example:

user1 has:

id    = user1
tags  = <apples, 100>, <bananas, 80>, <oranges, 60>
name  = "Ann Miller"
likes = "apples and skydiving, and sometimes solving puzzles"
quote = "Never spent a day without a smile!"

user2 has:

id    = user2
tags  = <bananas, 100>, <pears, 80>, <apples, 60>
name  = "Mike Anderson"
likes = "sleep and eating pizza"
quote = "Math, it's a puzzle to me. I love figuring out puzzles."

I want a general search that returns the user, something in the likes of:

search  |  result order
--------+--------------------
smile   | user1
puzzle  | user2, user1
bananas | user2, user1
apples  | user1, user2

Currently I have a general filed to witch I copy the values of each of the fields, and then I make the search on that field. Now I only put the key of the pairs in it, so the ordering is not the one I want.

I use SolrJ to index the document (extracted from a database) and to search.

I have thought of repeating the words like 100 times for apples, 80 for bananas, etc, but that strikes me as extremely and painfully slow (besides not being a good way to make it).

Do any of you guys have any idea?


Solution

  • Working on Andrea's suggestion, a solution could be in these lines:

    • Index the values as dynamic fields (e.g. *_value as in banana_value).
    • Using the eDisMax query parser boost or bf your search results using something like bf=field($value_field) where value_field is an additional param that you specify using SolrJ. Through this param you will provide the query term + the dynamic field suffix (e.g. banana_value) and, thanks of the function query field you will obtain the value for that particular pair and boost the result accordingly.