sorting - ElasticSearch过滤器对排序的影响

标签 sorting elasticsearch filtering

我在 Elasticsearch 中有一个奇怪的行为。在开始使用过滤器之前,我有一个查询:

查询不带过滤器

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "_facility": {
              "query": "error",
              "operator": "AND"
            }
          }
        },
        {
          "match": {
            "_application": {
              "query": "live",
              "operator": "AND"
            }
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "created_at": {
        "sort_mode": null,
        "order": "desc",
        "missing": null,
        "ignore_unmapped": null
      }
    },
    {
      "_score": {
        "sort_mode": null,
        "order": null,
        "missing": null,
        "ignore_unmapped": null
      }
    }
  ]
}

然后我必须添加过滤器

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "_facility": {
              "query": "error",
              "operator": "AND"
            }
          }
        },
        {
          "match": {
            "_application": {
              "query": "live",
              "operator": "AND"
            }
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "filter": {
    "and": {
      "filters": [
        {
          "range": {
            "created_at": {
              "from": 1373320800,
              "to": 1373493599,
              "include_lower": true,
              "include_upper": true
            },
            "_cache": true
          }
        }
      ]
    },
    "_cache": false
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "created_at": {
        "sort_mode": null,
        "order": "desc",
        "missing": null,
        "ignore_unmapped": null
      }
    },
    {
      "_score": {
        "sort_mode": null,
        "order": null,
        "missing": null,
        "ignore_unmapped": null
      }
    }
  ]
}

我还有下一个问题:

1)结果未按created_at正确排序,看起来像是混洗的数据

2)大小-不考虑任何不同于10的自定义值(假设我要显示20 [或5]个记录,但我有10个)

感谢您的帮助。可能我在Elastic Search概念中缺少了一些东西。

最佳答案

问题在于"_cache": false过滤器中的and实际上在过滤器之外。它应该更深一层:

"filter": {
  "and": {
    "filters": [{
      "range": {
        "created_at": {
          "from": 1373320800,
          "to": 1373493599,
          "include_lower": true,
          "include_upper": true
        },
        "_cache": true
      }
    }],
    "_cache": false
  }
}

它抛出Elasticsearch解析器,并开始忽略其余查询。顺便说一句,这些_cache语句是无用的,因为它们只是确保了默认设置。正如@Damien所说,整个请求将比filtered查询好得多:
{
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [{
            "match": {
              "_facility": {
                "query": "error",
                "operator": "AND"
              }
            }
          }, {
            "match": {
              "_application": {
                "query": "live",
                "operator": "AND"
              }
            }
          }]
        }
      },
      "filter": {
        "and": {
          "filters": [{
            "range": {
              "created_at": {
                "from": 1373320800,
                "to": 1373493599,
                "include_lower": true,
                "include_upper": true
              },
              "_cache": true
            }
          }, {
            "match_all": {}
          }],
          "_cache": false
        }
      }
    }

  },
  "from": 0,
  "size": 10,
  "sort": [{
    "created_at": {
      "order": "desc"
    }
  }, {
    "_score": {}
  }]
}

关于sorting - ElasticSearch过滤器对排序的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574835/

相关文章:

asp.net - Web 应用程序中的复杂过滤

mongodb - 过滤和排序 以下哪些查询将使用索引?

ruby-on-rails - Tire - Elasticsearch - 如何在创建时跳过索引?

elasticsearch - Elasticsearch轻松更新唯一元素列表

elasticsearch - Elasticsearch节点未获得任何碎片-如何诊断原因?

python - 如何实现像 scipy.signal.lfilter 这样的过滤器

ios - GPUImage GPUImageChromaKeyBlendFilter 与静止图像

shell - 仅对文件的前 100 行进行排序

sorting - 如何像 FogBugz 案例表一样对数据表进行排序

Ruby - Spaceship 运算符不会在 .sort 的 block 中工作