function findBestMatchingIndex(selector, userFields, sortOrder, indexes, useIndex) {

  var matchingIndexes = findMatchingIndexes(selector, userFields, sortOrder, indexes);

  if (matchingIndexes.length === 0) {
    if (useIndex) {
      throw {
        error: "no_usable_index",
        message: "There is no index available for this selector."
      };
    }
    //return `all_docs` as a default index;
    //I'm assuming that _all_docs is always first
    var defaultIndex = indexes[0];
    defaultIndex.defaultUsed = true;
    return defaultIndex;
  }
  if (matchingIndexes.length === 1 && !useIndex) {
    return matchingIndexes[0];
  }

  var userFieldsMap = arrayToObject(userFields);

  function scoreIndex(index) {
    var indexFields = index.def.fields.map(getKey);
    var score = 0;
    for (var i = 0, len = indexFields.length; i < len; i++) {
      var indexField = indexFields[i];
      if (userFieldsMap[indexField]) {
        score++;
      }
    }
    return score;
  }

  if (useIndex) {
    var useIndexDdoc = '_design/' + useIndex[0];
    var useIndexName = useIndex.length === 2 ? useIndex[1] : false;
    var index = matchingIndexes.find(function (index) {
      if (useIndexName && index.ddoc === useIndexDdoc && useIndexName === index.name) {
        return true;
      }

      if (index.ddoc === useIndexDdoc) {
        return true;
      }

      return false;
    });

    if (!index) {
      throw {
        error: "unknown_error",
        message: "Could not find that index or could not use that index for the query"
      };
    }
    return index;
  }

  return max(matchingIndexes, scoreIndex);
}
// in the user's query
in the user's query
in the user ' s query
function findBestMatchingIndex(selector, userFields, sortOrder, indexes, useIndex) {
  var matchingIndexes = findMatchingIndexes(selector, userFields, sortOrder, indexes);
  if (matchingIndexes.length === 0) {
    if (useIndex) {
      throw {
        error: "no_usable_index",
        message: "There is no index available for this selector."
      };
    }
    var defaultIndex = indexes[0];
    defaultIndex.defaultUsed = true;
    return defaultIndex;
  }
  if (matchingIndexes.length === 1 && !useIndex) {
    return matchingIndexes[0];
  }
  var userFieldsMap = arrayToObject(userFields);
  function scoreIndex(index) {
    var indexFields = index.def.fields.map(getKey);
    var score = 0;
    for (var i = 0, len = indexFields.length; i < len; i++) {
      var indexField = indexFields[i];
      if (userFieldsMap[indexField]) {
        score++;
      }
    }
    return score;
  }
  if (useIndex) {
    var useIndexDdoc = '_design/' + useIndex[0];
    var useIndexName = useIndex.length === 2 ? useIndex[1] : false;
    var index = matchingIndexes.find(function (index) {
      if (useIndexName && index.ddoc === useIndexDdoc && useIndexName === index.name) {
        return true;
      }
      if (index.ddoc === useIndexDdoc) {
        return true;
      }
      return false;
    });
    if (!index) {
      throw {
        error: "unknown_error",
        message: "Could not find that index or could not use that index for the query"
      };
    }
    return index;
  }
  return max(matchingIndexes, scoreIndex);
}
function findBestMatchingIndex ( selector , userFields , sortOrder , indexes , useIndex ) { var matchingIndexes = findMatchingIndexes ( selector , userFields , sortOrder , indexes ) ; if ( matchingIndexes . length === 0 ) { if ( useIndex ) { throw { error : "no_usable_index" , message : "There is no index available for this selector." } ; } var defaultIndex = indexes [ 0 ] ; defaultIndex . defaultUsed = true ; return defaultIndex ; } if ( matchingIndexes . length === 1 && ! useIndex ) { return matchingIndexes [ 0 ] ; } var userFieldsMap = arrayToObject ( userFields ) ; function scoreIndex ( index ) { var indexFields = index . def . fields . map ( getKey ) ; var score = 0 ; for ( var i = 0 , len = indexFields . length ; i < len ; i ++ ) { var indexField = indexFields [ i ] ; if ( userFieldsMap [ indexField ] ) { score ++ ; } } return score ; } if ( useIndex ) { var useIndexDdoc = '_design/' + useIndex [ 0 ] ; var useIndexName = useIndex . length === 2 ? useIndex [ 1 ] : false ; var index = matchingIndexes . find ( function ( index ) { if ( useIndexName && index . ddoc === useIndexDdoc && useIndexName === index . name ) { return true ; } if ( index . ddoc === useIndexDdoc ) { return true ; } return false ; } ) ; if ( ! index ) { throw { error : "unknown_error" , message : "Could not find that index or could not use that index for the query" } ; } return index ; } return max ( matchingIndexes , scoreIndex ) ; }
in the user's query
in the user ' s query
//return `all_docs` as a default index; //I'm assuming that _all_docs is always first