以下使用 Elasticsearch 的 GET /order/_search API 查询订单数据,并以可口可乐为例,演示如何使用 nested 查询,以及如何分析查询结果是否符合预期。

查询请求:

{
    "from": 1,
    "size": 100,
    "query": {
        "nested": {
            "query": {
                "match": {
                    "goodsName": {
                        "query": '可口可乐',
                        "operator": "AND",
                        "prefix_length": 0,
                        "max_expansions": 50,
                        "fuzzy_transpositions": true,
                        "lenient": false,
                        "zero_terms_query": "NONE",
                        "auto_generate_synonyms_phrase_query": true,
                        "boost": 1
                    }
                }
            },
            "path": "goodsInfo",
            "ignore_unmapped": true,
            "score_mode": "max",
            "boost": 1
        }
    }
}

查询结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "order",
                "_type": "_doc",
                "_id": "v-2DBokBo8NhfPsbYR_G",
                "_score": 1,
                "_source": {
                    "createTime": 1688031420655,
                    "goodsInfo": [
                        {
                            "goodsCount": 10,
                            "goodsName": "啤酒 500ml",
                            "price": 8.5
                        },
                        {
                            "goodsCount": 15,
                            "goodsName": "红酒 300ml",
                            "price": 20.5
                        }
                    ],
                    "orderNum": "20230628",
                    "userName": "qianqi"
                }
            },
            {
                "_index": "order",
                "_type": "_doc",
                "_id": "VQEPAIkBXi5tD6d44trb",
                "_score": 1,
                "_source": {
                    "createTime": 1687923186005,
                    "goodsInfo": [
                        {
                            "goodsCount": 10,
                            "goodsName": "可口可乐 500ml",
                            "price": 3.5
                        },
                        {
                            "goodsCount": 15,
                            "goodsName": "百世可乐 300ml",
                            "price": 2.5
                        }
                    ],
                    "orderNum": "20230628",
                    "userName": "qianqi"
                }
            }
        ]
    }
}

分析结果:

根据查询条件,获取到了两个订单数据。其中,第一个订单中的商品名称为'啤酒 500ml'和'红酒 300ml',与查询条件不相关。第二个订单中的商品名称为'可口可乐 500ml'和'百世可乐 300ml',与查询条件相关。

总结:

通过使用 nested 查询,可以有效地查询包含特定商品的订单数据,并根据实际情况分析查询结果是否符合预期。

Elasticsearch 查询订单数据:以可口可乐为例

原文地址: https://www.cveoy.top/t/topic/o4hs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录