基于键的ElasticSearch结果排序

euoag5mw  于 2023-04-29  发布在  ElasticSearch
关注(0)|答案(2)|浏览(124)

我正在尝试从ElasticSearch中获取数据。但无法分类结果。
以下是搜索查询:

{
    "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属性并返回原始结果时工作正常。

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

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

"hits": [
            {
                "_index": "search",
                "_type": "search",
                "_id": "218321",
                "_score": 1.0,
                "_source": {
                    "orderId": 211,
                    "commission_waived": 0,
                    "shippingTreeId": null,
                    "email": "manoj@xyz.in",
                    "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
                }
            }
]

任何人都可以纠正我在ElasticSearch查询中遗漏的地方。

qgzx9mmu

qgzx9mmu1#

Tldr;

这是一个Map问题。字段email必须使用text类型。
默认情况下,字段数据在 * 文本字段 * 上禁用。
什么是现场数据?
默认情况下,文本字段是可搜索的,但默认情况下不能用于聚合、排序或脚本编写。如果您尝试使用脚本对文本字段中的值进行排序、聚合或访问,您将看到一个异常,指示默认情况下在文本字段中禁用字段数据。

溶液

使用email.keyword

如果您没有手动设置Map,则很可能已经:

  • 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"
                        }
                    }
                }
            ]
        }
    }
}

修改Map+重索引

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

然后,您可以在search_2中的第一个索引search中重新索引数据

POST _reindex
{
  "source": {
    "index": "search"
  },
  "dest": {
    "index": "search_2"
  }
}

使用fielddata=true

使用文本字段来执行聚合/排序并不是最有效的方法。
将字段数据加载到存储器中会消耗大量存储器。

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

pqwbnv8z2#

可能是这个错误消息显示电子邮件字段是文本字段,意味着你需要启用fielddata选项。您可以更新索引的Map,例如:

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

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

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

Map之后,您可以编写如下查询:

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

相关问题