多级术语查询Elasticsearch

时间:2019-09-24 14:11:40

标签: elasticsearch

我想创建一个与“ sup_code”:“ X4595093”相匹配的Elasticsearch查询。原始查询是

{
    "query": {
        "bool": {
            "must": [
                {
                    "has_parent": {
                        "parent_type": "doc",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "id": [
                                                33172
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "created": {
                                                "lte": "now/d"
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "expires": {
                                                "gte": "now/d"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "term": {
                        "doc_type": "item"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "have_prices": true
                                }
                            },
                            {
                                "term": {
                                    "is_folder": true
                                }
                            }
                        ]
                    }
                },
                {
                    "term": {
                        "id": 254702776
                    }
                }
            ],
            "must_not": {
                "exists": {
                    "field": "folder"
                }
            }
        }
    },
    "sort": [
        {
            "is_folder": {
                "order": "desc"
            }
        },
        {
            "title_low.order": {
                "order": "asc"
            }
        }
    ],
   "size": 1000 
}

运行此查询时,得到以下响应:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": null,
        "hits": [
            {
                "_index": "prices",
                "_type": "doc",
                "_id": "item-254702776",
                "_score": null,
                "_routing": "1",
                "_source": {
                    "id": 254702776,
                    "id_pricedoc": 33172,
                    "path": null,
                    "supplier": {
                        "id": 89587333,
                        "type": "online"
                    },
                    "title": "00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123",
                    "is_folder": false,
                    "prices": [
                        {
                            "value": "По запросу",
                            "id_prcknd": 6622,
                            "currency": "RUR",
                            "id_offer": 604818674
                        },
                        {
                            "value": "По запросу",
                            "id_prcknd": 6622,
                            "currency": "RUR",
                            "id_offer": 604915992
                        }
                    ],
                    "market": {
                        "title": null,
                        "uuid": null,
                        "folder": null,
                        "path": null
                    },
                    "have_prices": true,
                    "folder": null,
                    "_join_field_name": "doc_type",
                    "pricedoc_created": "2019-06-19",
                    "supplier_nom": {
                        "manufacturer": {
                            "title": null,
                            "id": null
                        },
                        "id": 107549048,
                        "codes": null,
                        "market_nom": {
                            "id": null
                        },
                        "sup_code": "X4595093"
                    },
                    "doc_type": {
                        "parent": "doc-33172",
                        "name": "item"
                    },
                    "title_low": "00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123"
                },
                "sort": [
                    0,
                    "00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123 00012123"
                ]
            }
        ]
    }
}

现在,我想指定一个匹配“ sup_code”的查询:“ X4595093”。查询如下:

{
    "query": {
        "bool": {
            "must": [
                {
                    "has_parent": {
                        "parent_type": "doc",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "terms": {
                                            "id": [
                                                33172
                                            ]
                                        }
                                    },
                                    {
                                        "range": {
                                            "created": {
                                                "lte": "now/d"
                                            }
                                        }
                                    },
                                    {
                                        "range": {
                                            "expires": {
                                                "gte": "now/d"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                },
                {
                    "term": {
                        "doc_type": "item"
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "have_prices": true
                                }
                            },
                            {
                                "term": {
                                    "is_folder": true
                                }
                            }
                        ]
                    }
                },
                {
                    "terms": {
                        "supplier_nom.sup_code": ["X4754995"]
                    }
                }
            ],
            "must_not": {
                "exists": {
                    "field": "folder"
                }
            }
        }
    },
    "sort": [
        {
            "is_folder": {
                "order": "desc"
            }
        },
        {
            "title_low.order": {
                "order": "asc"
            }
        }
    ],
   "size": 1000 
}

此查询获得零点击。有人可以向我解释我在想什么吗? Sup_code是一个文本字段,而不是嵌套字段。

1 个答案:

答案 0 :(得分:0)

由于Sup_code是一个文本字段,因此您可以尝试这样

"supplier_nom.sup_code.keyword": ["X4754995"]