在ElasticSearch DSL中对不同文档进行排序

时间:2019-06-13 18:03:28

标签: elasticsearch dsl

我在ElasticSearch中索引了三种类型的文档:CarBikeTruck。我编写了一个查询来检索符合特定条件的所有文档,但是我也想仅使用Car具有和不具有的属性来排序检索Car的顺序。在BikeTruck中找到。

到目前为止,这就是我所拥有的:

   {
    "query": {
        "bool": {
            "must": [{
                "match": {
                    "is_registered": true
                }
            }]
        }
    },
    "sort": [{
        "ac_type": {
            "order": "desc"
        }
    }]
}

但是我在Bikes上遇到一个错误,该错误不具有属性ac_type

"type": "query_shard_exception",
"reason": "No mapping found for [bike.ac_type] in order to sort on",

如何检索所有三种文档类型,而仅根据单个文档中出现的字段进行排序?

汽车的索引映射:

{
    "car": {
        "aliases": {},
        "mappings": {
            "event": {
                "properties": {
                    "ac_type": {
                        "type": "long"
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1549617583913",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "cuwdmQbBTLeaSpkvzW4kPw",
                "version": {
                    "created": "6030299"
                },
                "provided_name": "car"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以做的一件事是在所有文档中添加'ac_type'参数。对于汽车,您可以为此参数添加值,对于其他人则不添加任何值。您可以使用此值对包含数据的文档进行排序,而对于其他文档则无所谓。