如何使用过滤器字段值过滤ID?

时间:2016-03-02 08:03:01

标签: elasticsearch

如何添加其他过滤条件以匹配category所有字段中的blog.post.notes值?首先,我想按ID过滤,然后过滤笔记类别,是否可能?

我只能通过ID过滤:

GET posts/posts/_search?fields=_id&_source=blog.post.notes
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "ids": {
          "values": [
            "100000000001234"
          ]
        }
      }
    }
  }
}

如何过滤,例如当前结果中的“测试”类别:

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

curl -XGET localhost:9200 / posts / _mapping / posts

{
  "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 :(得分:1)

您可以在必须使用ID和条款

时使用bool查询
POST c1_2/Test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "ids": {
            "values": [
              1,
              2,
              3
            ]
          }
        },
        {
          "terms": {
            "blog.post.notes.main.category": [
              "categoryfilter"
            ]
          }
        }
      ]
    }
  }
}

但是由于你有主要和购物车类别,你必须在每个类别上使用过滤器,在我的例子中我过滤主要类别,如果你需要对两者进行过滤,你需要再使用一个或过滤器将过滤主要类别或购物车类别

此外,您应该知道该类别应该不被分析,以便过滤类似"我的超级类别"其他明智的查询将无法正常工作。

实施例

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