02 ES5.0之后检索语法的变化

01 过滤检索

检索需求:查询姓氏为 "smith" ,且年龄大于30的员工

ES5之前的检索语法如下:

{
  "query":{
    "filtered":{
      "filter":{
        "range":{
          "age":{"gt":30}
        }	
      },
      "query":{
        "match":{"last_name":"smith"}
      }
    
    }
  
  }
}

  ES5之后,语法变为

{
  "query":{
    "bool":{
      "filter":{
        "range":{
          "age":{"gt":30}
        }	
      },
      "must":{
        "match":{"last_name":"smith"}
      }
    
    }
  
  }
}

  

02 聚合分析

执行下面的聚合分析,会报异常:  illegal_argument_exception

POST /megacorp/employee/_search


{
  "aggs":{
    "all_interests":{
      "terms":{"field":"interests"}
      }
  }
}

  问题出在5.x版本后对排序、聚合等操作为单独的数据结构缓存到内存里了,需要手动开启,官方文档作了说明:

https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

根据文档要求,我们在这里应该执行:

PUT megacorp/_mapping
{
  "properties": {
    "interests": { 
      "type":     "text",
      "fielddata": true
    }
  }
}

  执行之后,该字段interests就可以使用聚合函数了,比如all_interests,avg_interests等等

原文地址:https://www.cnblogs.com/lpzh/p/14091792.html