ElasticSearch范围查询

6ovsh4lw  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(122)

我遇到了另一个问题:如何使用rangeQuery搜索工资下限('salaryBottom ')和工资上限('salaryTop')?比如12到13岁的工资我尝试了几种方法,但都不起作用,结果是返回ID= 578的数据。
ES的ElasticSearch查询

{
    "from": 0,
    "size": 20,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "companyId": 3211002
                    }
                },
                {
                    "terms": {
                        "status": [
                            "0"
                        ]
                    }
                },
                {
                    "bool": {
                        "must": {
                            "range": {
                                "salaryBottom": {
                                    "from": "12",
                                    "to": null,
                                    "include_lower": true,
                                    "include_upper": false
                                }
                            }
                        }
                    }
                },
                {
                    "bool": {
                        "must": {
                            "range": {
                                "salaryTop": {
                                    "from": null,
                                    "to": "13",
                                    "include_lower": false,
                                    "include_upper": true
                                }
                            }
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "createTime": {
                "order": "desc"
            }
        }
    ]
}

结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": null,
        "hits": [
            {
                "_index": "recruitment",
                "_type": "requirement",
                "_id": "578",
                "_score": null,
                "_source": {
                    "id": 578,
                    "companyId": 3211002,
                    "hrId": 1004483,
                    "formId": 578,
                    "requirementId": "urge0001",
                    "degree": 7,
                    "positionTitle": "testJDL",
                    "positionProperties": 1,
                    "positionPriority": 1,
                    "requirementCount": 3,
                    "requirementType": 1,
                    "reportTo": 1004483,
                    "requirementTeam": 188384773,
                    "salaryTop": -1,
                    "salaryBottom": 33,
                    "requirementStatus": 0,
                    "manager": [
                        1004483
                    ],
                    "startTime": "2023-06-03",
                    "endTime": "2023-06-03",
                    "description": "333",
                    "status": 0,
                    "createTime": 1685689854630,
                    "hasAttachment": true
                },
                "sort": [
                    1685689854630
                ]
            },
            {
                "_index": "recruitment",
                "_type": "requirement",
                "_id": "563",
                "_score": null,
                "_source": {
                    "id": 563,
                    "companyId": 3211002,
                    "hrId": 1004483,
                    "formId": 563,
                    "requirementId": "testData",
                    "positionTitle": "afs",
                    "positionProperties": 2,
                    "requirementCount": 11,
                    "salaryTop": 13,
                    "salaryBottom": 12,
                    "requirementStatus": 0,
                    "description": "AbCdE的",
                    "status": 0,
                    "createTime": 1685606170594,
                    "hasAttachment": true
                },
                "sort": [
                    1685606170594
                ]
            }
        ]
    }
}
ogq8wdun

ogq8wdun1#

因为salaryBottom在逻辑上小于salaryTop,所以我会这样做(即12 <= salaryBottom < salaryTop <= 13):

{
                "bool": {
                    "must": {
                        "range": {
                            "salaryBottom": {
                                "from": "12",
                                "to": "13",
                                "include_lower": true,
                                "include_upper": false
                            }
                        }
                    }
                }
            },
            {
                "bool": {
                    "must": {
                        "range": {
                            "salaryTop": {
                                "from": "12",
                                "to": "13",
                                "include_lower": false,
                                "include_upper": true
                            }
                        }
                    }
                }
            }

相关问题