2

I want to add sorting by "field1" if it's exists in document, and by "field2" if not. Help me please with query syntax.

  • Do you mean you have two different types of document in your collection - one that has field1 and another that has field2? Or do you mean that all documents have both field1 and field2 but sometimes field1 has no data in it? – jb62 Oct 28 '16 at 15:40
  • I don't have the Admin UI in front of me right now, but I'd try a query that looks at both fields may be all you need... I'd try it on the Admin UI first - using the edismax - inside of which you can use the qf field where you can enter field1, field2 I believe... – jb62 Oct 28 '16 at 15:52
  • Something like this: qf="field1^2 field2" The "^" boosts field1 by a bit (2) and that boost should only apply if there is something in field1. – jb62 Oct 28 '16 at 16:51
  • I mean 2 different types of documents - first has field1, second has field2 – Sergei Vatshekin Oct 28 '16 at 18:23
  • It's a complex query with many conditions, and i need to add sort parameter as i described above. I'm thinking i need something like sort=if(exist(field1), ..., ...).... – Sergei Vatshekin Oct 28 '16 at 18:37
  • Have you tried that? Also, Not sure Solr supports if(exist ...) syntax. If you're getting both kinds of docs back and you want all the field1 docs at the top, you might try sort=field1, field2... I'm assuming that will put all results with field1 at the top, and ignore field1 if there aren't any docs with that field in them... but I'd have to test myself to be sure. (Personally, with complex stuff, I take out only the piece I'm working on and get it doing what I want before adding it back to the main, complex query.) – jb62 Oct 28 '16 at 19:22
  • any idea on how to solve this problem for string, text_general fields? `sortMissingLast` is an option when dealing with only one field in the sort query, but it doesn't do your intended action when having field1 not present, but field2 present.As a workaround I am thinking about concatenating the fields into another field used only for sorting. – marius_neo Oct 28 '16 at 21:18

1 Answers1

2

As already pointed in this SO question

try to use something like this for int fields :

sort=min(def(A_160018,9000000),def(A_chandigarh1,9000000))

For string fields the option would be to use in the core/collection an extra field containing the concatenation of the fields - e.g. fullname: lastname + ' ' + firstname written explicitly in the document when it is created. Then you can still do sort=fullname asc

Community
  • 1
  • 1
marius_neo
  • 1,535
  • 1
  • 13
  • 28
  • 1
    I need it for string fields, and new combined field is not suitable for this task (because fields for sorting can be changed dynamically, and in this case i must to rebuild these combined fields for all documents on every change) – Sergei Vatshekin Oct 29 '16 at 11:01
  • 1
    The way that the sorting is currently built in lucene, doesn't allow anything else than doubles to be used for searching. This is the reason why the sort function `if(exists(firstname), firstname, lastname) asc` can't work and solr returns `UnsupportedOperationException` at `FunctionValues#doubleVal(int)` method. Just as a hint `name asc` is handled as `ord(name) asc` (which returns a double value) – marius_neo Oct 31 '16 at 18:43
  • 1
    thanks for help, i created my own function (based on default DefFunction) with string type comparator – Sergei Vatshekin Nov 09 '16 at 07:20
  • 1
    can you add some sample query to see how did you solved your problem? – marius_neo Nov 11 '16 at 15:22
  • 1
    ...sort=getField(field1,field2) asc. For getField i used sources of DefFunction, changed: ` private FunctionValues get(int doc) { for (int i = 0; i < upto; i++) { FunctionValues vals = valsArr[i]; if (vals.exists(doc)) { return vals; } } return valsArr[upto - 1]; } ......` And comparator: `class ValueSourceComparator extends SimpleFieldComparator .....` – Sergei Vatshekin Dec 11 '16 at 08:50
  • Hi. I have the same problem, can you share details of the implementation of your getField function? Thanks. – calin014 Mar 13 '17 at 17:12