如何通过id获取并过滤嵌套数据,只留下按类别过滤的对象?

时间:2016-03-03 09:18:25

标签: elasticsearch

如何通过ID和过滤笔记只留下给定的类别?

数据:

POST c1_2/Blog/1
{
  "post": {
    "notes": {
      "main": [
        {
          "message": "blablabla",
          "category": "test"
        },
        {
          "message": "blablabla",
          "category": "other"
        }
      ],
      "cart": [
        {
          "message": "blablabla",
          "category": "test"
        },
        {
          "message": "blablabla",
          "category": "other"
        }
      ]
    }
  }
}

POST c1_2/Blog/2
{
  "post": {
    "notes": {
      "main": [
        {
          "message": "blablabla",
          "category": "second"
        },
        {
          "message": "blablabla",
          "category": "third"
        }
      ],
      "cart": [
        {
          "message": "blablabla",
          "category": "test"
        },
        {
          "message": "blablabla",
          "category": "other"
        }
      ]
    }
  }
}

搜索:

POST c1_2/Blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "ids": {
            "values": [
              1,
              2,
              3
            ]
          }
        },
        {
          "terms": {
            "post.notes.main.category": [
              "test"
            ]
          }
        }
      ]
    }
  }
}

当前结果,注释main / cart中的对象未按类别过滤:

{
   "took": 9,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1.0122644,
      "hits": [
         {
            "_index": "c1_2",
            "_type": "Blog",
            "_id": "1",
            "_score": 1.0122644,
            "_source": {
               "post": {
                  "notes": {
                     "main": [
                        {
                           "message": "blablabla",
                           "category": "test"
                        },
                        {
                           "message": "blablabla",
                           "category": "other"
                        }
                     ],
                     "cart": [
                        {
                           "message": "blablabla",
                           "category": "test"
                        },
                        {
                           "message": "blablabla",
                           "category": "other"
                        }
                     ]
                  }
               }
            }
         }
      ]
   }
}

期望效果:

"notes": {
   "main": [
      {
         "message": "blablabla",
         "category": "test"
      }
   ],
   "cart": [
      {
         "message": "blablabla",
         "category": "test"
      }
   ]
}

在我的真实应用中,查询嵌入"过滤"和"过滤",如果我将查询放在上面而不是过滤" ids"如下例所示,那么它会返回相同的数据吗?

POST c1_2/Blog/_search 
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "ids": {
          "values": [
            "1",
            "2"
          ]
        }
      }
    }
  }
}

映射:

{
  "posts": {
    "mappings": {
      "posts": {
        "dynamic_templates": [{
          "blog": {
            "mapping": {
              "index": "analyzed"
            },
            "path_match": "blog.*",
            "path_unmatch": "*.medias.*"
          }
        }, {
          "ids": {
            "mapping": {
              "index": "not_analyzed",
              "type": "string"
            },
            "match": "_id|base_id",
            "match_pattern": "regex"
          }
        }],
        "_all": {
          "enabled": false
        },
        "properties": {
          "query": {
            "properties": {
              "filtered": {
                "properties": {
                  "filter": {
                    "properties": {
                      "ids": {
                        "properties": {
                          "values": {
                            "type": "string"
                          }
                        }
                      }
                    }
                  },
                  "query": {
                    "properties": {
                      "match_all": {
                        "type": "object"
                      }
                    }
                  }
                }
              },
              "match_all": {
                "type": "object"
              }
            }
          },
          "source": {
            "dynamic": "true",
            "properties": {
              "post": {
                "dynamic": "true",
                "properties": {
                  "_id": {
                    "type": "string",
                    "index": "not_analyzed"
                  },
                  "base_id": {
                    "type": "string",
                    "index": "not_analyzed"
                  }
                }
              }
            }
          },
          "blog": {
            "properties": {
              "post": {
                "properties": {
                  "_id": {
                    "type": "string"
                  },
                  "notes": {
                    "properties": {
                      "main": {
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "message": {
                            "type": "string"
                          },
                          "category": {
                            "type": "string"
                          }
                        }
                      },
                      "cart": {
                        "properties": {
                          "id": {
                            "type": "string"
                          },
                          "message": {
                            "type": "string"
                          },
                          "category": {
                            "type": "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": {
            "terms": {
              "_id": [1, 2]
            }
          },
          "must_not": {
            "terms": {
              "post.notes.main.category": [
                "other"
              ]
            }
          }
        }
      }
    }
  }
}