rethink.go
func (s RethinkStore) getRootTerm(store string, filter map[string]interface{}, opts ObjectStoreOptions, args ...interface{}) (rootTerm r.Term) {
	rootTerm = r.DB(s.Database).Table(store)
	var hasIndex = false
	var hasMultiIndex = false
	var indexName string
	var indexVal string
	if opts != nil {
		indexes := opts.GetIndexes()
		logger.Info("indexed", "s", indexes)
		for name := range indexes {
			if val, ok := filter[name].(string); ok {
				hasIndex = true
				indexVal = val
				indexName = name
				ix_id_name := name + "_id"
				if _, ok := indexes[ix_id_name]; ok {
					indexName = ix_id_name
					hasMultiIndex = true
					break
				}
				break
			}
		}
	}
	if !hasIndex {
		if len(args) == 0 {
			rootTerm = rootTerm.OrderBy(
				r.OrderByOpts{Index: r.Desc("id")})
		}
	} else {
		if hasMultiIndex {
			rootTerm = rootTerm.Between(
				[]interface{}{indexVal, r.MinVal},
				[]interface{}{indexVal, r.MaxVal},
				r.BetweenOpts{Index: indexName, RightBound: "closed"}).OrderBy(r.OrderByOpts{Index: r.Desc(indexName)})
		} else {
			rootTerm = rootTerm.GetAllByIndex(indexName, indexVal)
		}
	}
	if len(filter) > 0 {
		rootTerm = rootTerm.Filter(s.transformFilter(nil, filter))
	}
	return
}
func (s RethinkStore) getRootTerm(store string, filter map[string]interface{}, opts ObjectStoreOptions, args ...interface{}) (rootTerm r.Term) {
	rootTerm = r.DB(s.Database).Table(store)
	var hasIndex = false
	var hasMultiIndex = false
	var indexName string
	var indexVal string
	if opts != nil {
		indexes := opts.GetIndexes()
		logger.Info("indexed", "s", indexes)
		for name := range indexes {
			if val, ok := filter[name].(string); ok {
				hasIndex = true
				indexVal = val
				indexName = name
				ix_id_name := name + "_id"
				if _, ok := indexes[ix_id_name]; ok {
					indexName = ix_id_name
					hasMultiIndex = true
					break
				}
				break
			}
		}
	}
	if !hasIndex {
		if len(args) == 0 {
			rootTerm = rootTerm.OrderBy(
				r.OrderByOpts{Index: r.Desc("id")})
		}
	} else {
		if hasMultiIndex {
			rootTerm = rootTerm.Between(
				[]interface{}{indexVal, r.MinVal},
				[]interface{}{indexVal, r.MaxVal},
				r.BetweenOpts{Index: indexName, RightBound: "closed"}).OrderBy(r.OrderByOpts{Index: r.Desc(indexName)})
		} else {
			rootTerm = rootTerm.GetAllByIndex(indexName, indexVal)
		}
	}
	if len(filter) > 0 {
		rootTerm = rootTerm.Filter(s.transformFilter(nil, filter))
	}
	return
}
// http://stackoverflow.com/questions/19747207/rethinkdb-index-for-filter-orderby
//TODO: fix index selection, it should favour compound indexes more