elasticsearch - 弹性搜索 - 过滤多个 OR 条件的条件
问题描述
您好,我是 Elastic Search 的新手,很难理解它是如何工作的。假设我在弹性搜索服务器中有 20 种产品。每个产品对象看起来像这样
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 27,
"relation" : "eq"
},
"hits" : [
{
"_index" : "ddddd",
"_type" : "_doc",
"_id" : "1062985",
"_score" : 1.0,
"_source" : {
"productId" : "1234567890",
"name" : "Cakes",
"slug" : "cakes",
"adId" : 1062985,
"shortDescription" : "My teddy Bear",
"longDescription" : "",
"totalRating" : 0,
"totalReviewCount" : 0,
"attributeSet" : "609e11c66b66fe030d94f400",
"mapDetails" : {
"addressComponents" : {
"country" : {
"shortName" : "IN",
"longName" : "India"
},
"administrativeAreaLevel1" : {
"shortName" : "KL",
"longName" : "Kerala"
},
"administrativeAreaLevel2" : {
"shortName" : "TVM",
"longName" : "Thiruvananthapuram"
},
"locatlity" : {
"shortName" : "",
"longName" : ""
},
"sublocalityLevel1" : {
"shortName" : "Ulloor",
"longName" : "Ulloor"
},
"sublocalityLevel2" : {
"shortName" : "",
"longName" : ""
},
"route" : {
"shortName" : "SH 1",
"longName" : "State Highway 1"
},
"postalCode" : {
"shortName" : "695015",
"longName" : "695015"
}
},
"location" : [
8.5241391,
76.9366376
],
"locationString" : "206, SH 1, Ulloor, Thiruvananthapuram, Kerala 695015, India"
},
"category" : [
{
"catId" : "60b47e0cf511114e4a9107d8",
"isCatgegory" : true
},
{
"catId" : "root",
"isParentCatgegory" : true
}
]
}
]
}
}
现在我需要实现的是根据日期对它们进行排序,然后使用对象中的特征标记从中过滤掉特征。然后使用多个 OR 条件从该过滤器中
我尝试的是
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"term": {
"featured": {
"value": "true"
}
}
},
{
"bool": {
"filter": [
{
"terms": {
"category.catId": [
"60c2c8c1031ee362a67e1316",
"609e1a546b66fe030d94f402",
"609e1ad46b66fe030d94f405",
"60b47e0cf511114e4a9107d8"
]
}
}
]
}
},
{
"bool": {
"filter": [
{
"nested": {
"path": "mapDetails.addressComponents.country",
"query": {
"term": {
"mapDetails.addressComponents.country.shortName": {
"value": "IN"
}
}
}
}
}
]
}
}
]
}
},
"sort": [
{
"featured": {
"order": "desc"
}
},
{
"updatedAt": {
"order": "desc"
}
}
]
}
但这会为匹配类别和 mapDetails.addressComponents.country 的特色项目提供结果
我想要的是特色和(类别或 mapDetails.addressComponents.country)
试过 1
{
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"term": {
"featured": {
"value": "true"
}
}
}
],
"should": [
{
"bool": {
"filter": [
{
"nested": {
"path": "mapDetails.addressComponents.country",
"query": {
"term": {
"mapDetails.addressComponents.country.shortName": {
"value": "IN"
}
}
}
}
}
]
}
},
{
"bool": {
"filter": [
{
"terms": {
"category.catId": [
"60c2c8c1031ee362a67e1316"
]
}
}
]
}
}
]
}
},
"sort": [
{
"featured": {
"order": "desc"
}
},
{
"updatedAt": {
"order": "desc"
}
}
]
}
结果 :: 它给出的结果不属于类别或位置
有人可以指出我正确的方向吗?
提前致谢 任何帮助表示赞赏。
解决方案
您应该使用 should 子句。应该作为 OR
{
"from":0,
"size":10,
"query":{
"bool":{
"must":[
{
"term":{
"featured":{
"value":"true"
}
}
},
{
"bool":{
"should":[
{
"terms":{
"category.catId":[
"60c2c8c1031ee362a67e1316",
"609e1a546b66fe030d94f402",
"609e1ad46b66fe030d94f405",
"60b47e0cf511114e4a9107d8"
]
}
},
{
"nested":{
"path":"mapDetails.addressComponents.country",
"query":{
"term":{
"mapDetails.addressComponents.country.shortName":{
"value":"IN"
}
}
}
}
},
"minimum_should_match":1
]
}
}
]
}
},
"sort":[
{
"featured":{
"order":"desc"
}
},
{
"updatedAt":{
"order":"desc"
}
}
]
}
推荐阅读
- python - 在较长列表的每个第 n 个元素处将两个不同长度的列表组合成元组
- c# - Unity Android C++ DllNotFoundException
- visual-studio-code - 在不同端口上使用 VS Code 调试 2 个 nodemon 实例
- sql - 仅在满足某些条件时才尝试强制转换列
- r - 在 data.table 的区间内按日期选择行
- python - SQL/Python (Django) - 将每一行加入整个表
- java - 基于运输时间的热图/轮廓(反向等时轮廓)
- java - JwtBuilder 类的 @autowired 不起作用,错误:需要找不到类型为“io.jsonwebtoken.JwtBuilder”的 bean
- c#-4.0 - 特定异常的 Hangfire 自动重试
- r - Shiny DT appearance messed up when selected rows used as reactive values