[ 
https://issues.apache.org/jira/browse/SOLR-3926?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13505483#comment-13505483
 ] 

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 :-)

{code}
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();
}
{code}

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

{code}
// Usage, per the api above
query.setSort(SolrQuery.SortClause.desc("rating"));
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"));
query.remove("rating");
{code}


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.

{code}
// 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));
query.removeSort("rating");

// 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
{code}


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:

{code}
// 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");
{code}


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?

{code}
// Q5: Should we REMOVE asc() and desc() convenience factory methods:
query.setSort(SolrQuery.SortClause.desc("rating"));
query.setSort(SolrQuery.SortClause.asc("rating"));

// 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")));
{code}

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 name?
Q11: Should we have SortClause.hashCode() and SortClause.equals()?
                
> solrj should support better way of finding active sorts
> -------------------------------------------------------
>
>                 Key: SOLR-3926
>                 URL: https://issues.apache.org/jira/browse/SOLR-3926
>             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: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org
For additional commands, e-mail: dev-h...@lucene.apache.org

Reply via email to