假设这是一个映射,而人是type
:
{
"person" : {
"properties" : {
"name" : {
"type" : "object",
"properties" : {
"first" : {"type" : "string"},
"last" : {"type" : "string"}
}
}
}
}
}
如何按姓氏对返回的person
文件进行排序?在_search
查询中,我尝试:
{
"sort": [ {"name.last": "desc" } ]
}
更新提防非常长的问题
@pickypg绝对正确,以上工作。所以这是我更复杂的结构,但却没有。映射:
curl -XPUT "http://localhost:9200/venue/event/_mapping" -d '
{
"event" : {
"properties" : {
"name" : { "type" : "string" },
"address" : { "type" : "string"},
"_id" : {
"type" : "object",
"properties" : {
"$oid" : { "type" : "string" }
}
},
"events" : {
"type" : "object",
"properties": {
"event_id" : {
"properties" : {
"$oid" : { "type" : "string" }
}
},
"title" : { "type" : "string"},
"start_date" : { "type" : "date" , "format": "basic_date_time_no_millis" }
}
}
}
}
}'
我在venue/event/_mapping
上添加文档之前收到的内容(_id
和$oid
丢失了):
{
venue: {
mappings: {
event: {
properties: {
name: {
type: "string"
},
address: {
type: "string"
},
events: {
properties: {
event_id: {
properties: {
$oid: {
type: "string"
}
}
},
start_date: {
type: "date",
format: "basic_date_time_no_millis"
},
title: {
type: "string"
}
}
}
}
}
}
}
}
我插入的示例文档是什么(通过bulksearch-py中的批量Feed:helpers.bulk()
):
{
'_index': 'venue',
'_type': 'event',
'_id': '52cb45cfc36b444275172903',
'_source': {
'name': 'S.WalterStewartLibrary',
'address': '170MemorialParkAvenue',
'_id': {
'$oid': '52cb45cfc36b4442751728f9'
},
'events': {
'title': u'ParentLedSingAlongGroup',
'event_id': {
'$oid': '52cb45cfc36b444275172903'
},
'start_date': u'2014-01-08T20: 15: 00+00: 00',
}
}
}
我在venue/event/_mapping
上添加文档后得到的结果。 $oid
已移至_id
的父级:
{
venue: {
mappings: {
event: {
properties: {
$oid: {
type: "string"
},
address: {
type: "string"
},
events: {
properties: {
event_id: {
properties: {
$oid: {
type: "string"
}
}
},
start_date: {
type: "date",
format: "basic_date_time_no_millis"
},
title: {
type: "string"
}
}
},
name: {
type: "string"
}
}
}
}
}
}
查询:
这可能有用(因为结果中的排序显示数字):
GET venue/event/_search
{
"size": 2,
"sort": [
{
"name": "asc"
}
]
}
结果:
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
},
"hits": {
"total": 1000,
"max_score": null,
"hits": [
{
"_index": "venue",
"_type": "event",
"_id": "531de9d1c36b442c7cf79981",
"_score": null,
"_source": {
"name": "Springville Moose Lodge 14141",
"_id": {
"$oid": "531de9d0c36b442c7cf7997c"
},
"events": {
"title": "Community Meeting, Film Screening & Fish Fry",
"event_id": {
"$oid": "531de9d1c36b442c7cf79981"
},
"start_date": "2014-03-14T19:00:00-04:00"
}
},
"sort": [
"14141"
]
},
{
"_index": "venue",
"_type": "event",
"_id": "5303e066c36b4453b45fe459",
"_score": null,
"_source": {
"name": "AnCafe 173 Xuân Thủy",
"address": "173 Xuân Thủy",
"_id": {
"$oid": "5303e066c36b4453b45fe458"
},
"events": {
"end_date": "2014-03-01T10:00:00+00:00",
"title": "2014: Plans for WordPress in Vietnam",
"event_id": {
"$oid": "5303e066c36b4453b45fe459"
},
"start_date": "2014-03-01T08:00:00+00:00"
}
},
"sort": [
"173"
]
}
]
}
}
这绝对不是:
GET venue/event/_search
{
"size": 2,
"sort": [
{
"events.title": "asc"
}
]
}
结果(排序为空):
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"failed": 0
},
"hits": {
"total": 1000,
"max_score": null,
"hits": [
{
"_index": "venue",
"_type": "event",
"_id": "52e83eb6c36b4473d6c177ba",
"_score": null,
"_source": {
"name": "Canadian Academic Succes School",
"address": "5310A Yonge Street",
"_id": {
"$oid": "52cb45cec36b4442751728f6"
},
"events": {
"title": "Valentine's Party",
"event_id": {
"$oid": "52e83eb6c36b4473d6c177ba"
},
"start_date": "2014-02-16T01:00:00+00:00"
}
},
"sort": [
null
]
},
{
"_index": "venue",
"_type": "event",
"_id": "52f70fa6c36b446c340e9374",
"_score": null,
"_source": {
"name": "Canadian Academic Succes School",
"address": "5310A Yonge Street",
"_id": {
"$oid": "52cb45cec36b4442751728f6"
},
"events": {
"title": "English Circle 1 (With Brian)",
"event_id": {
"$oid": "52f70fa6c36b446c340e9374"
},
"start_date": "2014-02-10T00:00:00+00:00"
}
},
"sort": [
null
]
}
]
}
}
答案 0 :(得分:1)
For string based types, the field sorted on should not be analyzed / tokenized.
因此,对于长度超过一个值的字符串(例如名称Sir Ker
),不应对其进行分析以进行排序。一个好方法是使用新的copy_to,新字段有"index": "not_analyzed"
curl -s -XGET 'es:9200/users/journalist/_mapping'
{
"journalist": {
"properties": {
"name": {
"type": "string",
"copy_to" : "untouched_name"
},
"untouched_name": {
"index": "not_analyzed",
"type": "string"
}
}
}
}
然后要求对untouched_name
进行排序。
但我认为最好的是fields:
"name": {
"type": "string",
"index": "analyzed",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
},
并排序:name.raw: asc
现在我正在搜索如何对 date
类型字段进行排序。我会添加更多。
看起来日期很好,不需要像字符串那样进行分析。