是否可以在Elasticsearch中使用doc_values=true过滤非索引字段

dw1jzc5e  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(3)|浏览(84)

在Elasticsearch 5.6中使用以下Map:

"category" => [
    "type"=>"keyword",
    "doc_values"=>true,
    "index"=>false
    "store"=>true
]

字符串
我得到的建议是,由于doc_values设置,可以编写一个过滤此字段的查询,即使index属性设置为false,但似乎doc_values字段只对聚合和排序有用。
是否可以创建一个过滤此字段的查询?

dy1byipe

dy1byipe1#

没有索引的字段是by definition,不可搜索。Elasticsearch不会将其放入倒排索引(用于搜索)。如果您尝试运行搜索查询,您将得到类似Cannot search on field [category] since it is not indexed.的错误

uqxowvwt

uqxowvwt2#

Keyword数据类型默认启用doc_values。您不需要显式启用它。和关键字字段可以过滤查询,如果确切的文本搜索。例如,我有以下索引和Map

PUT index_name
{
  "mappings": {
    "type_name": {
      "properties": {
        "id": {
          "type": "long"
        },
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}

字符串
样本文件:

{
    "id":1,
    "name":"hatim"
}


所以我可以在查询部分这样过滤搜索

GET index_name/type_name/_search
{
  "query": {
    "term": {
      "name": "hatim"
    }
  }
}


等等,你可以像这样在上面添加聚合

GET index_name/type_name/_search
{
  "query": {
    "term": {
      "name": "hatim"
    }
  },
  "aggs": {
    "count": {
      "value_count": {
        "field": "name"
      }
    }
  }
}

0pizxfdo

0pizxfdo3#

当然,不能使用索引来过滤未建立索引的字段。但是,在某些情况下,您可能知道数据量很低(因为您使用其他索引字段(如time或else)过滤数据),并且仍然希望在另一个未索引字段的帮助下仅返回过滤数据的子集。
在这种情况下,也只有在这种情况下,您可以使用脚本过滤器:

GET /_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": """
            double amount = doc['amount'].value;
            if (doc['type'].value == 'expense') {
              amount *= -1;
            }
            return amount < 10;
          """
        }
      }
    }
  }
}

字符串
请参阅docs了解更多关于它所暗示的权衡的信息。

相关问题