基于key的弹性搜索排序结果

问题描述 投票:0回答:2

我正在尝试从弹性搜索中获取数据。 但无法对结果进行排序。

这里是搜索查询:

{
    "size": 20,
    "from": 0,
    "sort": {
        "email": {
            "order": "desc"
        }
    },
    "query": {
        "bool": {
            "must": [
                "",
                {
                    "range": {
                        "created_at": {
                            "gte": "2017-01-01",
                            "lte": "now"
                        }
                    }
                }
            ]
        }
    }
}

回复:

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [email] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "search",
                "node": "XLaHCsHWR9WHIFXgT5o7nw",
                "reason": {
                    "type": "illegal_argument_exception",
                    "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [email] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
                }
            }
        ],
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [email] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.",
            "caused_by": {
                "type": "illegal_argument_exception",
                "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [email] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            }
        }
    },
    "status": 400
}

当我删除排序属性并返回原始结果时,上面的搜索查询工作正常。

"sort": {
        "email": {
            "order": "desc"
        }
    },

类似的输出看起来像一个对象数组。

        "hits": [
            {
                "_index": "search",
                "_type": "search",
                "_id": "218321",
                "_score": 1.0,
                "_source": {
                    "orderId": 211,
                    "commission_waived": 0,
                    "shippingTreeId": null,
                    "email": "[email protected]",
                    "userId": 787,
                    "firstName": "manoj",
                    "currency": "USD",
                    "item_quantity": 5,
                    "lastName": "manoj",
                    "fullName": "manoj manoj",
                    "affiliatedBy": 10452,
                    "affiliatedByName": "manoj manoj",
                    "office_specialist": null,
                    "grandTotal": "101.03",
                    "conversionType": "ct0",
                    "created_at": "2023-04-28T04:14:12.000Z",
                    "transactionId": "cf_seffdghghkjgd54564",
                    "orderStatus": "Processing",
                    "status": "0",
                    "carrier": null,
                    "tracking_number": null,
                    "paymentStatus": "Paid",
                    "shipment_tree_id": null,
                    "warehouse_name": null,
                    "warehouse_id": null,
                    "updated_at": "2023-04-28T04:14:14.000Z",
                    "shipment_url": null,
                    "couponCode": "testing23",
                    "id": "2181",
                    "paymentStatusId": 0
                }
            }
]

任何人都可以纠正我在弹性搜索查询中遗漏的地方。

javascript node.js amazon-web-services elasticsearch kibana
2个回答
0
投票

长话短说;

这是一个映射问题。 您的字段

email
必须使用
text
类型。

Fielddata 在 text fields 默认情况下被禁用。

什么是现场数据

文本字段默认是可搜索的,但默认情况下不可用于聚合、排序或脚本。如果您尝试使用脚本对文本字段中的值进行排序、聚合或访问,您将看到一个异常,表明文本字段默认禁用字段数据。

解决方案

使用
email.keyword

如果您没有手动设置映射,您可能有:

  • email
    作为
    text
  • email.keyword
    作为
    keyword
    子字段

在那种情况下,您只需要:

{
    "size": 20,
    "from": 0,
    "sort": {
        "email.keyword": {
            "order": "desc"
        }
    },
    "query": {
        "bool": {
            "must": [
                "",
                {
                    "range": {
                        "created_at": {
                            "gte": "2017-01-01",
                            "lte": "now"
                        }
                    }
                }
            ]
        }
    }
}

更改映射+重新索引

PUT search_2
{
  "mappings": {
    "properties": {
      "email":{
        "type": "keyword"
      }
      .
      .
      .
    }
  }
}

然后可以在

search
中重新索引第一个索引
search_2

中的数据
POST _reindex
{
  "source": {
    "index": "search"
  },
  "dest": {
    "index": "search_2"
  }
}

使用 fielddata=true

使用文本字段进行聚合/排序并不是最有效的方法。

在内存中加载字段数据会消耗大量内存。

PUT /search/
{
  "mappings": {
    "properties": {
      "email":{
        "type": "text",
        "fielddata": true
      }
    }
  }
}

0
投票

可能是此错误消息显示电子邮件字段是文本字段,意味着您需要启用字段数据选项。您可以更新索引的映射,例如:

"email": {
      "type": "text",
      "fielddata": true
    }

第二种方法是创建字段并设置关键字:

{
  "properties": {
    "email": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword"
        }
      }
    }
  }
}

映射后,您编写如下查询:

{
    "size": 20,
    "from": 0,
    "sort": {
        "email.keyword": {
            "order": "desc"
        }
    },
    "query": {
        "bool": {
            "must": [
                "",
                {
                    "range": {
                        "created_at": {
                            "gte": "2017-01-01",
                            "lte": "now"
                        }
                    }
                }
            ]
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.