这些elasticsearch查询有什么区别?

时间:2020-08-20 21:02:56

标签: elasticsearch

我有以下Elasticsearch查询,返回的结果很多。

{
    "query": {
        "multi_match": {
            "query": "swartz",
            "fields": ["notes"]
        }
    },
    "size": 20,
    "from": 0,
    "sort": {
        "last_modified_date": {
            "order": "desc"
        }
    }
} 

我正在尝试将其重做为布尔查询,以便可以添加should和must_not,但是没有得到结果,我不确定为什么。

{
    "query": {
        "bool": {
            "must": [
                {"term": { "notes": "swartz" }}
            ]
        }
    },
    "size": 20,
    "from": 0,
    "sort": {
        "last_modified_date": {
            "order": "desc"
        }
    }
}

我得到的是结果,而不是结果。

  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 1,
    "failures" : [
      {
        "shard" : 0,
        "index" : ".kibana_1",
        "node" : "E2fjoon_Smm5m7LFcQp9XQ",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "No mapping found for [last_modified_date] in order to sort on",
          "index_uuid" : "0pZdhm_nRXWiWGcqFgvvHQ",
          "index" : ".kibana_1"
        }
      }
    ]
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

首先,我不确定为什么会得到结果,并且它对第一个查询的排序正确,其次,即使我从第二个查询中删除了排序,也仍然没有结果。

1 个答案:

答案 0 :(得分:0)

首先,您使用匹配查询将在“注释”内容中的某处查找任何出现的“ swartz”。

在SQL世界中,它类似于:

where notes ilike "%swartz%"

在第二个查询中,您将使用术语查询,该查询将在字段中寻找完美的相等性。 在SQL中:

 where "notes"=="swartz"

它可能可以解释您的行为 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html