Thanks Chris ! I have the country as a single valued field so your solution works perfectly !
On Fri, Aug 28, 2015 at 1:22 PM, Chris Hostetter <hossman_luc...@fucit.org> wrote: > > : I have a "country" field in my index, with values like 'US', 'FR', 'UK', > : etc... > : > : Then I want our users to be able to define the order of their preferred > : countries so that grouped results are sorted according to their > preference. > ... > : Is there any other function that would allow me to map from a predefined > : String constant into an Integer that I can sort on ? > > Because of how they evolved, and most of the common usecases for them, > there aren't a lot of functions that operate on "strings". > > Assuming your "country" field is a single valued (indexed) string field, > then what you want can be done fairly simply using the the "termfreq()" > function. > > termfreq(country,US) will return the (raw integer) term frequency for > "Term(country,US)" for each doc -- assuming it's single valued (and not > tokenized) that means for every doc it will be either a 0 or a 1. > > so you can either modify your earlier attempt at using "map" on the string > values to do a map over the termfreq output, or you can simplify things to > just multiply take the max value -- where max is just a short hand for > "the non 0 value" ... > > max(mul(9,termfreq(country,US)), > mul(8,termfreq(country,FR)), > mul(7,termfreq(country,UK)), > ...) > > Things get more interesting/complicated if the field isn't single valued, > or is tokenized -- then individual values (like "US") might have a > termfreq that is greater then 1, or a doc might have more then one value, > and you have to decide what kind of math operation you want to apply over > those... > > * ignore termfreqs and ony look at if term exists? > - wrap each termfreq in map to force value to either 0 or 1 > * want to sort by sum of (weights * termfreq) for each term? > - change max to sum in above example > * ignore all but the "main" term that has hte highest freq for each doc? > - not easy at query time - best to figure out the "main" term at index > time and put in it's own field. > > > -Hoss > http://www.lucidworks.com/ >