Elastic Search RangeQuery

huangapple go评论79阅读模式
英文:

Elastic Search RangeQuery

问题

ElasticSearch查询的JSON代码已经翻译如下:

{
    "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
                ]
            }
        ]
    }
}
英文:

I have encountered another issue: how do I use rangeQuery to search for the lower limit of salary ('salaryBottom') and the upper limit of salary ('salaryTop')? like salary between 12 and 13.
I tried several methods, but none of them worked, and the result was to return the data with ID= 578.

ElasticSearch Query for ES

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

Result:

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

答案1

得分: 1

由于 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
}
}
}
}
}
}
英文:

Since salaryBottom is logically smaller than salaryTop, I would do it this way (i.e. 12 <= salaryBottom < salaryTop <= 13):

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

huangapple
  • 本文由 发表于 2023年6月5日 17:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定