什么是 ES Nested 嵌套类型? Elasticsearch 有很多数据类型,大致如下:
nested 类型是一种特殊的 [object,object] 类型. 这种类型允许对 object 数组内的元素进行单独查询,区别是:
嵌套模型的优缺点如下:
动态映射 字段数据类型不需要事先定义,ES内部自动映射默认字段类型。如果是object类型,数据同步时会把内部的text映射成text,还会带上keyowd的子类型。参考
object类型的filed会把text类型字段没有加上keyword,就是不分词。所以我们改成neatest类型。 因嵌套对象(nested objects)会被索引为分离的隐藏文档,我们不能直接查询它们。而是使用 nested查询或 nested 过滤器来存取它们:
GET /my_index/blogpost/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "eggs" }}, <1>
{
"nested": {
"path": "comments", <2>
"query": {
"bool": {
"must": [ <3>
{ "match": { "comments.name": "john" }},
{ "match": { "comments.age": 28 }}
]
}}}}
]
}}}
PUT /my_index/blogpost/2
{
"title": "Investment secrets",
"body": "What they don't tell you ...",
"tags": [ "shares", "equities" ],
"comments": [
{
"name": "Mary Brown",
"comment": "Lies, lies, lies",
"age": 42,
"stars": 1,
"date": "2014-10-18"
},
{
"name": "John Smith",
"comment": "You're making it up!",
"age": 28,
"stars": 2,
"date": "2014-10-16"
}
]
}
嵌套查询
GET /_search
{
"query": {
"nested": {
"path": "comments",
"filter": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
},
"sort": {
"comments.stars": {
"order": "asc",
"mode": "min",
"nested_filter": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
}
}
上面的方案在ES6.1之后是不行的,因为嵌套类型的排序有特殊的排序nested sort选项,开启的方式和netsed查询很像。所以我们升级了一下
POST /_search
{
"query" : {
"term" : { "product" : "chocolate" }
},
"sort" : [
{
"offer.price" : {
"mode" : "avg",
"order" : "asc",
"nested": {
"path": "offer",
"filter": {
"term" : { "offer.color" : "blue" }
}
}
}
}
]
}
java代码的写法
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("custom_info.deptUpdateTime");
rangeQueryBuilder.gte(condition.getDeptUpdateTime().getStartTime() + "T00:00:00");
rangeQueryBuilder.lte(condition.getDeptUpdateTime().getEndTime() + "T23:59:59");
boolBuilder.must(rangeQueryBuilder);
QueryBuilder nestedQueryBuilder = new NestedQueryBuilder(
"custom_info",
boolBuilder,
ScoreMode.None
);
netst——path
ElasticSearch的Nested(嵌套)数据类型