gpt4 book ai didi

elasticsearch - 按内部字段字符串对文档进行排序

转载 作者:行者123 更新时间:2023-12-02 23:40:17 25 4
gpt4 key购买 nike

假设这是一个映射,person是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"
}
}
}
}
}
}
}
}

我插入的示例文档是什么(通过批量Feed:elasticsearch-py中的 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"
}
]
}

结果(排序为null):
{
"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
]
}
]
}
}

最佳答案

documentation:

For string based types, the field sorted on should not be analyzed / tokenized.

因此,对于长度超过一个值的字符串(例如,名称 Sir Ker),不应对其进行分析以进行排序。一个好的方法是使用新的 copy_to,并且新字段具有 "index": "not_analyzed"
示例 based on this:
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类型字段进行排序。我会添加更多。

看起来日期很好,不需要像字符串那样对它们进行分析。

One more interesting source from ES forum

关于elasticsearch - 按内部字段字符串对文档进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com