lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eirik Lygre (JIRA)" <>
Subject [jira] [Commented] (SOLR-3926) solrj should support better way of finding active sorts
Date Wed, 28 Nov 2012 14:30:59 GMT


Eirik Lygre commented on SOLR-3926:

I'll take the blame for guiding Yonik down the Map-path; at the time (while parsing the sort-field),
returning a LinkedHashMap was an easy way to achieve the business objectives. Then, as the
idea developed, it became less so. Anyway, that's why we review, right?

Here is an extended view of my current implementation. It will probably not be like this,
ref questions below :-)

public String getSortField();

public SolrQuery setSorts(List<SortClause> value);
public SolrQuery clearSorts();
public List<SortClause> getSorts();
public SolrQuery setSort(SortClause sortClause);
public SolrQuery addSort(SortClause sortClause);
public SolrQuery addOrUpdateSort(SortClause sortClause);
public SolrQuery removeSort(String itemName);

public static class SortClause {
  public static SortClause create (String item, ORDER order);
  public static SortClause create (String item, String order)
  public static SortClause asc (String item);
  public static SortClause desc (String item);
  public String getItem();
  public ORDER getOrder();

Some questions, illustrated by code examples. Some questions relate to apis shown above, and
are REMOVE? questions; some questions relate to apis *not* shown above, and are ADD? questions.
Note that some of the examples use stuff from other

// Usage, per the api above
query.setSort(SolrQuery.SortClause.create("rating", SolrQuery.ORDER.desc));
query.setSort(SolrQuery.SortClause.create("rating", SortQuery.ORDER.valueOf("desc")));
query.setSort(SolrQuery.SortClause.create("rating", "asc"));

I want to retain query.removeSort(String), because that's really the use case (remove sort
based on item name, ignoring ordering). I'm not really sure about query.removeSort(SortClause),
which does in fact only use the item name, but it would be symmetrical to the add-functions.

// Q1: Should we REMOVE query.removeSort (String)
query.addSort(new SolrQuery.SortClause("rating", SolrQuery.ORDER.desc));
query.addSort(new SolrQuery.SortClause("price", SolrQuery.ORDER.asc));

// Q2: Should we ADD query.removeSort(SortClause)?
query.addSort(new SolrQuery.SortClause("rating", SolrQuery.ORDER.desc));
query.addSort(new SolrQuery.SortClause("price", SolrQuery.ORDER.asc));
query.removeSort(new SolrQuery.SortClause("price", SolrQuery.ORDER.desc));	// Remove irregardless
of order

We might build convenience functions query.xxxSort (String, order) and query.xxxSort (String,String)
as shown below. It would make usage simpler, but come with a footprint. The SortClause.asc(),
.desc() and .create() factory functions described below make this less needed, I think:

// Q3: Should we ADD convenience functions query.xxxSort (String, order)
query.addSort("price", SolrQuery.ORDER.asc);

// Q4: Should we ADD convenience functions query.xxxSort (String, String)
query.addSort("price", "asc");

The api currently has convenience functions for creating SortClause. The functions asc() and
desc() make it easier (and more compact) to create SortClause. The create() functions are
there for symmetry (always use static methods instead of constructors). The constructors aren't
public, but maybe they should be?

// Q5: Should we REMOVE asc() and desc() convenience factory methods:

// Q6: Should we REMOVE create(String,ORDER) convenience factory method (use constructor instead)
query.setSort(SolrQuery.SortClause.create("rating", SolrQuery.ORDER.desc));
query.setSort(SolrQuery.SortClause.create("rating", SolrQuery.ORDER.valueOf("desc")));

// Q7:Should we REMOVE create(String,ORDER) convenience factory method (Complements Q5, when
the order is in fact a string)
query.setSort(SolrQuery.SortClause.create("rating", "desc"));

// Q8: Should we ADD a simple constructor, typically instead of Q5-Q7?
query.setSort(new SolrQuery.SortClause("rating", SolrQuery.ORDER.desc));
query.setSort(new SolrQuery.SortClause("rating", SolrQuery.ORDER.valueOf("desc")));

A couple of other items:

Q9: Currently, SortClause is an inner class of SolrQuery. Let me know if this is an issue
Q10: What the heck do we call "the thing to sort". I don't want to call it a "field", since
it can be many other things. I've chosen to call it an "item", but is there another, better
Q11: Should we have SortClause.hashCode() and SortClause.equals()?
> solrj should support better way of finding active sorts
> -------------------------------------------------------
>                 Key: SOLR-3926
>                 URL:
>             Project: Solr
>          Issue Type: Improvement
>          Components: clients - java
>    Affects Versions: 4.0
>            Reporter: Eirik Lygre
>            Priority: Minor
>             Fix For: 4.1
>         Attachments: SOLR-3926.patch, SOLR-3926.patch, SOLR-3926.patch
> The Solrj api uses ortogonal concepts for setting/removing and getting sort information.
Setting/removing uses a combination of (name,order), while getters return a String "name order":
> {code}
> public SolrQuery setSortField(String field, ORDER order);
> public SolrQuery addSortField(String field, ORDER order);
> public SolrQuery removeSortField(String field, ORDER order);
> public String[] getSortFields();
> public String getSortField();
> {code}
> If you want to use the current sort information to present a list of active sorts, with
the possibility to remove then, you need to manually parse the string(s) returned from getSortFields,
to recreate the information required by removeSortField(). Not difficult, but not convenient
either :-)
> Therefore this suggestion: Add a new method {{public Map<String,ORDER> getSortFieldMap();}}
which returns an ordered map of active sort fields. This will make introspection of the current
sort setup much easier.
> {code}
>   public Map<String, ORDER> getSortFieldMap() {
>     String[] actualSortFields = getSortFields();
>     if (actualSortFields == null || actualSortFields.length == 0)
>       return Collections.emptyMap();
>     Map<String, ORDER> sortFieldMap = new LinkedHashMap<String, ORDER>();
>     for (String sortField : actualSortFields) {
>       String[] fieldSpec = sortField.trim().split(" ");
>       sortFieldMap.put(fieldSpec[0], ORDER.valueOf(fieldSpec[1]));
>     }
>     return Collections.unmodifiableMap(sortFieldMap);
>   }
> {code}
> For what it's worth, this is possible client code:
> {code}
> System.out.println("Active sorts");
> Map<String, ORDER> fieldMap = getSortFieldMap(query);
> for (String field : fieldMap.keySet()) {
>    System.out.println("- " + field + "; dir=" + fieldMap.get(field));
> }
> {code}

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message