Mongo不会在{$ match:{$ text:{$ search:search}}之后使用索引吗?

时间:2019-07-04 08:44:19

标签: mongodb

我有管道:

[ { '$match': { '$text': [Object] } },
{ '$addFields': { sortFieldTextRelevance: [Object] } },
{ '$match': { '$and': [Array] } } ]

我看到第二个匹配项的索引没有被使用,但是如果我删除了

 { $match: { $text: { $search: search } }

一切都很好。怎么了?

1 个答案:

答案 0 :(得分:1)

https://docs.mongodb.com/manual/core/aggregation-pipeline/#aggregation-pipeline-operators-and-performance读为:

  

$ match和$ sort管道运算符出现在管道的开始处时可以利用索引。

因此索引仅适用于第一阶段的比赛。

如果您可以创建compound text index,则可以通过将两个匹配项组合到第一阶段来使用它:

{
  "annotations": {
  "list": [
   {
    "builtIn": 1,
    "datasource": "-- Grafana --",
    "enable": true,
    "hide": true,
    "iconColor": "rgba(0, 211, 255, 1)",
    "name": "Annotations & Alerts",
    "type": "dashboard"
  }
 ]
},
"editable": true,
"gnetId": 1860,
"graphTooltip": 0,
"id": 35,
"iteration": 1562652036142,
"links": [],
"panels": [
 {
  "collapsed": false,
  "gridPos": {
    "h": 1,
    "w": 24,
    "x": 0,
    "y": 0
  },
  "id": 261,
  "panels": [],
  "repeat": null,
  "title": "General",
  "type": "row"
},
{
  "cacheTimeout": null,
  "colorBackground": false,
  "colorValue": false,
  "colors": [
    "rgba(245, 54, 54, 0.9)",
    "rgba(237, 129, 40, 0.89)",
    "rgba(50, 172, 45, 0.97)"
  ],
  "datasource": "Prometheus",
  "decimals": 2,
  "description": "System Load (1m avg)",
  "format": "short",
  "gauge": {
    "maxValue": 100,
    "minValue": 0,
    "show": false,
    "thresholdLabels": false,
    "thresholdMarkers": true
  },
  "gridPos": {
    "h": 3,
    "w": 4,
    "x": 0,
    "y": 1
  },
  "id": 17,
  "interval": null,
  "links": [],
  "mappingType": 1,
  "mappingTypes": [
    {
      "name": "value to text",
      "value": 1
    },
    {
      "name": "range to text",
      "value": 2
    }
  ],
  "maxDataPoints": 100,
  "maxPerRow": 12,
  "nullPointMode": "null",
  "nullText": null,
  "options": {},
  "postfix": "",
  "postfixFontSize": "50%",
  "prefix": "",
  "prefixFontSize": "50%",
  "rangeMaps": [
    {
      "from": "null",
      "text": "N/A",
      "to": "null"
    }
  ],
  "sparkline": {
    "fillColor": "rgba(31, 118, 189, 0.18)",
    "full": false,
    "lineColor": "rgb(31, 120, 193)",
    "show": false
  },
  "tableColumn": "",
  "targets": [
    {
      "expr": "node_load1{instance=~\"$node:$port\",job=~\"$job\"}",
      "hide": false,
      "intervalFactor": 1,
      "refId": "A",
      "step": 900
    }
  ],
  "thresholds": "",
  "title": "System Load (1m avg)",
  "type": "singlestat",
  "valueFontSize": "50%",
  "valueMaps": [
    {
      "op": "=",
      "text": "N/A",
      "value": "null"
    }
  ],
  "valueName": "current"
}
],
"refresh": false,
"schemaVersion": 18,
"style": "dark",
"tags": [],
"templating": {
"list": [
  {
    "allValue": null,
    "current": {
      "text": "limca",
      "value": "limca"
    },
    "datasource": "Prometheus",
    "definition": "",
    "hide": 0,
    "includeAll": false,
    "label": "Job",
    "multi": false,
    "name": "job",
    "options": [],
    "query": "label_values(node_uname_info, job)",
    "refresh": 1,
    "regex": "",
    "skipUrlSync": false,
    "sort": 1,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  },
  {
    "allValue": null,
    "current": {
      "text": "limca",
      "value": "limca"
    },
    "datasource": "Prometheus",
    "definition": "",
    "hide": 2,
    "includeAll": false,
    "label": "",
    "multi": false,
    "name": "name",
    "options": [],
    "query": "label_values(node_uname_info{job=~\"$job\"}, nodename)",
    "refresh": 1,
    "regex": "",
    "skipUrlSync": false,
    "sort": 1,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  },
  {
    "allValue": null,
    "current": {
      "text": "limca",
      "value": "limca"
    },
    "datasource": "Prometheus",
    "definition": "",
    "hide": 2,
    "includeAll": false,
    "label": "Host:",
    "multi": false,
    "name": "node",
    "options": [],
    "query": "label_values(node_uname_info{nodename=\"$name\"}, instance)",
    "refresh": 1,
    "regex": "/([^:]+):.*/",
    "skipUrlSync": false,
    "sort": 1,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  },
  {
    "allValue": null,
    "current": {
      "text": "9100",
      "value": "9100"
    },
    "datasource": "Prometheus",
    "definition": "",
    "hide": 0,
    "includeAll": false,
    "label": "Port",
    "multi": false,
    "name": "port",
    "options": [],
    "query": "label_values(node_uname_info{instance=~\"$node:(.*)\"}, instance)",
    "refresh": 1,
    "regex": "/[^:]+:(.*)/",
    "skipUrlSync": false,
    "sort": 3,
    "tagValuesQuery": "",
    "tags": [],
    "tagsQuery": "",
    "type": "query",
    "useTags": false
  }
]
 },
 "time": {
"from": "now-24h",
"to": "now"
 },
 "timepicker": {
"refresh_intervals": [
  "5s",
  "10s",
  "30s",
  "1m",
  "5m",
  "15m",
  "30m",
  "1h",
  "2h",
  "1d"
],
"time_options": [
  "5m",
  "15m",
  "1h",
  "6h",
  "12h",
  "24h",
  "2d",
  "7d",
  "30d"
]
},
"timezone": "browser",
"title": "importTest",
"uid": "importjson",
"version": 1
}