有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring boot Elasticsearch高级Rest客户端Java排序工作不正常

我是Elasticsearch高级Rest客户端(Java)的新手

我有一个非常简单的查询,其中列出了所有记录,但排序似乎没有正常工作

有些字段是文本类型,所以我需要将fielddata设置为true

更新:

谢谢安德烈的解决方案

我需要添加另一个字段,它是对象数组user.groups

我将.keyword添加到可排序的映射字段(文本)

但我得到了意想不到的结果

示例:

...
"groups": [
    {"name", "ECPay", ... },
    {"name", "Abangers", ... }
]
...

根据下面的输出,如果有两个组项ECPayAbangers。。。使用^ {CD5> }排序将考虑abanges。

我想把^ {CD1>}数组的第一个元素作为排序的基础。

根据上面的示例,tt将使用ECPay作为排序的基础,因为它是数组的第一个元素

要了解问题(更新),让我们检查下面的搜索结果(排序)

搜索结果(输出):

{
    "took": 18,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 74,
        "max_score": null,
        "hits": [
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "5",
                "_score": null,
                "_source": {
                    "name": "Ericsson Joseph Sultan Atutuli",
                    "country": "",
                    "uuid": "5",
                    "userId": 5,
                    "email": "ejsultanatutuli@gmail.com",
                    "deletedInd": false,
                    "groups": [
                        {
                            "name": "ECPay",
                            "id": 2
                        },
                        {
                            "name": "Abangers",
                            "id": 4
                        }
                    ],
                    "company": ""
                },
                "sort": [
                    "Abangers"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "54",
                "_score": null,
                "_source": {
                    "name": "Florentina Atutuli",
                    "country": null,
                    "uuid": "54",
                    "userId": 54,
                    "email": "florentina.atutuli@gmail.com",
                    "deletedInd": false,
                    "groups": [
                        {
                            "name": "Abangers",
                            "id": 4
                        },
                        {
                            "name": "Test Group",
                            "id": 5
                        }
                    ],
                    "company": null
                },
                "sort": [
                    "Abangers"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "37",
                "_score": null,
                "_source": {
                    "name": "dsfsdfsdf",
                    "country": null,
                    "uuid": "37",
                    "userId": 37,
                    "email": "asdf@sdf.com",
                    "deletedInd": false,
                    "groups": [
                        {
                            "name": "Abangers",
                            "id": 4
                        },
                        {
                            "name": "Test Group",
                            "id": 5
                        }
                    ],
                    "company": null
                },
                "sort": [
                    "Abangers"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "3",
                "_score": null,
                "_source": {
                    "name": "Erick Atutuli",
                    "country": "Philippines",
                    "email": "erickatutuli@pakyas.com",
                    "userId": 3,
                    "uuid": "d8f4ab43-d33e-4a82-a08b-eb73342a0546",
                    "groups": [
                        {
                            "name": "ECPay",
                            "id": 2
                        },
                        {
                            "name": "Abangers",
                            "id": 4
                        }
                    ],
                    "deletedInd": false,
                    "company": "Hotlegs Incorporated"
                },
                "sort": [
                    "Abangers"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "34",
                "_score": null,
                "_source": {
                    "name": "Chun-Li",
                    "country": null,
                    "email": "chunli@pakyas.com",
                    "uuid": "34",
                    "userId": 34,
                    "deletedInd": false,
                    "groups": [
                        {
                            "name": "Customers AU",
                            "id": 1
                        }
                    ],
                    "company": null
                },
                "sort": [
                    "Customers AU"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "57",
                "_score": null,
                "_source": {
                    "name": "Eddy Bear",
                    "country": "US",
                    "email": "eddybear@pakyas.com",
                    "uuid": "57",
                    "userId": 57,
                    "deletedInd": false,
                    "groups": [
                        {
                            "name": "Customers AU",
                            "id": 1
                        }
                    ],
                    "company": "Jollibee"
                },
                "sort": [
                    "Customers AU"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "42",
                "_score": null,
                "_source": {
                    "name": "Alfredo Pitik Mingaw",
                    "country": "",
                    "email": "akomykel@gmail.com",
                    "userId": 42,
                    "uuid": "42",
                    "deletedInd": false,
                    "groups": [
                        {
                            "name": "ECPay",
                            "id": 2
                        }
                    ],
                    "company": ""
                },
                "sort": [
                    "ECPay"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "20",
                "_score": null,
                "_source": {
                    "name": "test",
                    "country": "Åland Islands",
                    "userId": 20,
                    "email": "test102@email.com",
                    "uuid": "20",
                    "groups": [
                        {
                            "name": "ECPay",
                            "id": 2
                        }
                    ],
                    "deletedInd": false,
                    "company": "test"
                },
                "sort": [
                    "ECPay"
                ]
            },

            ...

        ]
    }
}

现在,我在用户映射中添加了keyword字段:

...
"groups": {
    "properties": {
        "name": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        }
    }
},
...

使文本字段可排序,删除字段数据并添加关键字

http://localhost:9200/acme_users/_mapping/user  

{
  "properties": {
        "name": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        },
        "company": {
            "type": "text",
            "fielddata": true
        },
        "country": {
            "type": "text",
            "fielddata": true
        },
        "groups": {
            "properties": {
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        },
        "email": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                }
            }
        }
  }
}

下面的查询是从我的Java应用程序(Spring)生成的
这是一个非常简单的例子,我只是将结果按nameascending顺序排序

查询

.keyword添加到排序字段。在下面的示例中。。。它将使用groups.name.keyword进行排序,它最初是groups.name

http://localhost:9200/acme_users/user/_search  

{
  "from" : 0,
  "size" : 15,
  "query" : {
    "match_all" : {
      "boost" : 1.0
    }
  },
  "sort" : [
    {
      "groups.name.keyword" : {
        "order" : "asc"
      }
    }
  ]
}

原始问题:

但我现在的问题是,似乎ES排序不能正常工作。当我将顺序从asc更改为desc(反之亦然)时,结果会发生变化
虽然结果发生了变化,但名称(result)似乎没有正确排序(a-z)或(z-a)
似乎它得到了name的最后一部分,并将其用作排序的基础。我想基于name的第一个字符值

原始搜索结果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 73,
        "max_score": null,
        "hits": [
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "22",
                "_score": null,
                "_source": {
                    "name": "Popeye Partner 01",
                    "country": null,
                    "userId": 22,
                    "email": "popeye_partner_01@kugmo.com",
                    "uuid": "22",
                    "deletedInd": false,
                    "company": null
                },
                "sort": [
                    "01"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "24",
                "_score": null,
                "_source": {
                    "name": "Dummy User 01",
                    "country": null,
                    "userId": 24,
                    "email": "dummy@dummy.com",
                    "uuid": "24",
                    "deletedInd": false,
                    "company": null
                },
                "sort": [
                    "01"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "23",
                "_score": null,
                "_source": {
                    "name": "Popeye Partner 02",
                    "country": null,
                    "userId": 23,
                    "email": "popeye_partner_02@kugmo.com",
                    "uuid": "23",
                    "deletedInd": false,
                    "company": null
                },
                "sort": [
                    "02"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "25",
                "_score": null,
                "_source": {
                    "name": "RT Administrator",
                    "country": null,
                    "userId": 25,
                    "email": "rt_administrator@kugmo.com",
                    "uuid": "25",
                    "deletedInd": false,
                    "company": null
                },
                "sort": [
                    "administrator"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "48",
                "_score": null,
                "_source": {
                    "name": "John Patrick Abnoy",
                    "country": null,
                    "userId": 48,
                    "email": "patrickabnoy@gmail.com",
                    "uuid": "48",
                    "deletedInd": false,
                    "company": null
                },
                "sort": [
                    "abnoy"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "5",
                "_score": null,
                "_source": {
                    "name": "Ericsson John Santol Atutuli",
                    "country": "",
                    "uuid": "5",
                    "userId": 5,
                    "email": "ejsantolatutuli@gmail.com",
                    "deletedInd": false,
                    "company": ""
                },
                "sort": [
                    "atutuli"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "54",
                "_score": null,
                "_source": {
                    "name": "Florentina Atutuli",
                    "country": null,
                    "uuid": "54",
                    "userId": 54,
                    "email": "florentina.atutuli@gmail.com",
                    "deletedInd": false,
                    "company": null
                },
                "sort": [
                    "atutuli"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "49",
                "_score": null,
                "_source": {
                    "name": "Laarnie Santol Atutuli",
                    "country": "",
                    "email": "lmsantolatutuli@gmail.com",
                    "userId": 49,
                    "uuid": "49",
                    "deletedInd": false,
                    "company": ""
                },
                "sort": [
                    "atutuli"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "3",
                "_score": null,
                "_source": {
                    "name": "Eric Atutuli",
                    "country": "Philippines",
                    "uuid": "d8f4ab43-d33e-4a82-a08b-eb73342a0546",
                    "userId": 3,
                    "email": "ericatutuli@kugmo.com",
                    "deletedInd": false,
                    "company": "Hotlegs Incorporated"
                },
                "sort": [
                    "atutuli"
                ]
            },
            {
                "_index": "acme_users",
                "_type": "user",
                "_id": "29",
                "_score": null,
                "_source": {
                    "name": "Auberto Matulis",
                    "country": null,
                    "userId": 29,
                    "email": "bert.matulis@gmail.com",
                    "uuid": "29",
                    "deletedInd": false,
                    "company": null
                },
                "sort": [
                    "auberto"
                ]
            }
        ]
    }
}

谢谢


共 (2) 个答案

  1. # 1 楼答案

    name是一个text字段,表示已对其进行分析。分析意味着它被分割成代币。如果将Popeye Partner 01拆分为popeyepartner01您希望使用哪个字段进行排序?可能没有,因为您希望对原始文本进行排序。为此,请在name字段中添加一个子字段

    {
      "name": {
        "type" "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
    

    然后在查询中使用它进行排序:

    "sort" : [  
        {  
          "name.keyword" : {  
            "order" : "asc"  
          }  
        }  
      ]
    
  2. # 2 楼答案

    我们遇到了同样的问题,然后我们查看了我们在indexCreation时创建的映射,我们观察到了一些事情

    1. 所有的字段总是有一个词作为值,应该改为关键字,所以我们不需要追加。所有字段名称中的关键字(不希望在客户端将架构附加到append.keyword)或添加"fielddata": true(尽可能避免此操作,因为它会占用堆内存) "type":"keyword"
    2. 所有的值都定义为文本,但实际上它们是数字,所以我们改为长浮点或缩放浮点。这有助于我们进行分类和汇总

    "type":"long"

    "type": "scaled_float", "scaling_factor": 100