我正在尝试索引一个文档,该文档具有三个字段 first_name、last_name、“关键字”类型的职业,并且分别具有值 XYZ、ABC、DEF。
我已经使用过滤器编写了与 AND 条件完全匹配的查询,如下所示,
"query": {
  "bool": {
    "filter": [
      {"term": {"first_name": "XYZ"}},
      {"term": {"last_name": "ABC"}}
    ]
  }
}
这必须返回一个文档,但什么都不返回。
我对同一操作有另一个查询,
"query": {
  "bool": {
    "must": [
      {"match": {"first_name": "XYZ"}},
      {"match": {"last_name": "ABC"}}
    ]
  }
}
这将返回一个文档。
根据 Elasticsearch 文档,我了解查询和过滤器之间的区别在于过滤器不会对结果进行评分。我不确定为什么第一个查询不返回任何结果。我的理解正确吗?
正如文档所述,除了评分之外,查询和过滤器之间没有任何区别。当然,这适用于查询和过滤器都使用相同查询类型的情况。这里您使用两种不同的类型 -term和match。term专为精确比较而设计,同时match进行分析和用作全文搜索。
看看下面的例子。
您的映射:
PUT /index_53053054
{
  "mappings": {
    "_doc": {
      "properties": {
        "first_name": {
          "type": "text"
        },
        "last_name": {
          "type": "text"
        },
        "occupation": {
          "type": "keyword"
        }
      }
    }
  }
}
您的文件:
PUT index_53053054/_doc/1
{
  "first_name": "XYZ",
  "last_name": "ABC",
  "occupation": "DEF"
}
filter询问:
GET index_53053054/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "first_name": "XYZ"
          }
        },
        {
          "match": {
            "last_name": "ABC"
          }
        },
        {
          "term": {
            "occupation": "DEF"
          }
        }
      ]
    }
  }
}
和结果:
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": [
      {
        "_index": "index_53053054",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "first_name": "XYZ",
          "last_name": "ABC",
          "occupation": "DEF"
        }
      }
    ]
  }
}
类似must查询:
GET index_53053054/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "first_name": "XYZ"
          }
        },
        {
          "match": {
            "last_name": "ABC"
          }
        },
        {
          "term": {
            "occupation": "DEF"
          }
        }
      ]
    }
  }
}
和回应:
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.8630463,
    "hits": [
      {
        "_index": "index_53053054",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.8630463,
        "_source": {
          "first_name": "XYZ",
          "last_name": "ABC",
          "occupation": "DEF"
        }
      }
    ]
  }
}
正如你所看到的,hits几乎是一样的。唯一的区别是 infilter分数不计算,而 inmust查询则计算。
了解更多:https ://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-filter-context.html
| 归档时间: | 
 | 
| 查看次数: | 7566 次 | 
| 最近记录: |