![](/img/trans.png)
[英]How can I run a search in my postgress database, return the result and then send it to be displayed on an ejs page?
[英]I worked on query of Elastic Search. But my result was not sorted by A to Z. So, how can I sort my result by A to Z
我在許多領域工作,以獲取按 a 到 z 排序的結果。 這是我寫的查詢:
query: {
multi_match: {
query: "m*",
fields: ["name", "name_mn"],
type: "phrase_prefix",
max_expansions: 50,
operator: "and",
// fields: ['title', 'authors.*name'],
// minimum_should_match: 3,
// analyzer: "standard",
// slop: 3,
},
結果
1 - Mary to Majo no Hana (score: 5.140061)
2 - Princess Mononoke (score: 4.0831547)
3 - Girl Like Me (score: 3.5263605)
4 - Love and Monsters (score: 3.5263605)
5 - Howl's Moving Castle (score: 3.5263605)
6 - Mairimashita! Iruma-kun S01 (score: 3.1031976)
7 - Before We Get Married (score: 3.1031976)
8 - Rick and Morty S01 (score: 3.1031976)
9 - Majo no Tabitabi (score: 3.0155346)
10 - In the mood for love (score: 2.770712)
我真正想要的排序如下:
1 - Mary to Majo no Hana (score: 5.140061)
2 - Mairimashita! Iruma-kun S01 (score: 3.1031976)
3 - Majo no Tabitabi (score: 3.0155346)
4 - Princess Mononoke (score: 4.0831547)
5 - Girl Like Me (score: 3.5263605)
6 - Love and Monsters (score: 3.5263605)
7 - Howl's Moving Castle (score: 3.5263605)
8 - Before We Get Married (score: 3.1031976)
9 - Rick and Morty S01 (score: 3.1031976)
10 - In the mood for love (score: 2.770712)
如果要對結果進行ascending
排序,基於句子的第一個字母,則需要對搜索結果進行升序排序
添加一個工作示例
索引映射:
{
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
搜索查詢:
{
"query": {
"multi_match": {
"query": "m*",
"type": "phrase_prefix"
}
},
"sort": {
"name.keyword": {
"order": "desc"
}
}
}
搜索結果:
"hits": [
{
"_index": "67651064",
"_type": "_doc",
"_id": "7",
"_score": null,
"_source": {
"name": "Before We Get Married"
},
"sort": [
"Before We Get Married"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "3",
"_score": null,
"_source": {
"name": "Girl Like Me"
},
"sort": [
"Girl Like Me"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "5",
"_score": null,
"_source": {
"name": "Howl's Moving Castle"
},
"sort": [
"Howl's Moving Castle"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "10",
"_score": null,
"_source": {
"name": "In the mood for love"
},
"sort": [
"In the mood for love"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "4",
"_score": null,
"_source": {
"name": "Love and Monsters"
},
"sort": [
"Love and Monsters"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "6",
"_score": null,
"_source": {
"name": "Mairimashita! Iruma-kun S01"
},
"sort": [
"Mairimashita! Iruma-kun S01"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "9",
"_score": null,
"_source": {
"name": "Majo no Tabitabi"
},
"sort": [
"Majo no Tabitabi"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "1",
"_score": null,
"_source": {
"name": "Mary to Majo no Hana"
},
"sort": [
"Mary to Majo no Hana"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "2",
"_score": null,
"_source": {
"name": "Princess Mononoke"
},
"sort": [
"Princess Mononoke"
]
},
{
"_index": "67651064",
"_type": "_doc",
"_id": "8",
"_score": null,
"_source": {
"name": "Rick and Morty S01"
},
"sort": [
"Rick and Morty S01"
]
}
]
@ESCoder 我在查詢下方添加了排序然后我想以 m 開頭
1 - Rick and Morty S01 (score: null)
2 - Princess Mononoke (score: null)
3 - Mary to Majo no Hana (score: null)
4 - Majo no Tabitabi (score: null)
5 - Mairimashita! Iruma-kun S01 (score: null)
6 - Love and Monsters (score: null)
7 - In the mood for love (score: null)
8 - Howl's Moving Castle (score: null)
9 - Girl Like Me (score: null)
10 - Before We Get Married (score: null)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.