如何在弹性搜索中使用 DSL QUERY 验证输入值是否在范围内
How to verify input value is within range or not in elastic search Using DSL QUERY
我有两个实体,例如 User & Activity。 Activity 包含像 ["10:20","25:30"] 这样的归档年龄组和包含年龄字段的用户实体。考虑到用户年龄是 26 岁,所以 he/she 符合 activity 的条件。
我想获取所有满足上述条件的活动
GET _search
{
"query": {
"bool": {
"filter": {
"range": {
"ageGroup": {
"gte": 50,
"lte": 55
}
},
"term": {"****": "M"}
},
"must": [
{
"term": {"******": {"value": "15"}}
}
] ,"should": [
{
"match": {"******": "7"}
},
{
"match": {"******": "1"}
}
]
}
}
}
包括(Entity/Object)包含列表
public class Include {
@Field(type= FieldType.Nested)
private List<AgeGroup> age;
}
数据:
{......, include.age:{ "id": 4, "minAge": 4, "maxAge": 9 }, { "id": 3, "minAge": 1, "maxAge": 4 }, .....}
DSL 查询:
GET /_search
{
"query": {
"bool": {
"should": [
{"range": {"include.age.minAge": {"lte": 9}}},
{"range": {"include.age.maxAge": {"gte": 9}}
}
],"minimum_should_match": 2
}
}
}
您还可以使用 integer_range 数据类型来验证值是否在范围内
https://www.elastic.co/guide/en/elasticsearch/reference/current/range.html
我有两个实体,例如 User & Activity。 Activity 包含像 ["10:20","25:30"] 这样的归档年龄组和包含年龄字段的用户实体。考虑到用户年龄是 26 岁,所以 he/she 符合 activity 的条件。 我想获取所有满足上述条件的活动
GET _search
{
"query": {
"bool": {
"filter": {
"range": {
"ageGroup": {
"gte": 50,
"lte": 55
}
},
"term": {"****": "M"}
},
"must": [
{
"term": {"******": {"value": "15"}}
}
] ,"should": [
{
"match": {"******": "7"}
},
{
"match": {"******": "1"}
}
]
}
}
}
包括(Entity/Object)包含列表
public class Include {
@Field(type= FieldType.Nested)
private List<AgeGroup> age;
}
数据: {......, include.age:{ "id": 4, "minAge": 4, "maxAge": 9 }, { "id": 3, "minAge": 1, "maxAge": 4 }, .....}
DSL 查询:
GET /_search
{
"query": {
"bool": {
"should": [
{"range": {"include.age.minAge": {"lte": 9}}},
{"range": {"include.age.maxAge": {"gte": 9}}
}
],"minimum_should_match": 2
}
}
}
您还可以使用 integer_range 数据类型来验证值是否在范围内 https://www.elastic.co/guide/en/elasticsearch/reference/current/range.html