【问题标题】:How to filter by ids together with filter fields value?如何通过 id 和过滤器字段值进行过滤?
【发布时间】:2016-03-02 22:01:08
【问题描述】:

如何添加额外的过滤器以匹配blog.post.notes 所有字段中的category 值?首先我想通过ids过滤,然后过滤notes类别,可以吗?

我只能按 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"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    您可以在 id 和术语上使用带有 must 的 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"
                ]
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 感谢您花时间详细说明,我尝试在过滤器和查询部分添加此内容,但它返回任何类别的 0 个命中,无论如何,如果没有其他提示,我会将您的答案标记为正确。
    • 好吧,我在发布之前尝试了这个查询,它必须有效。你能复制你使用的查询吗?
    • 'category' 位于 main 数组中的对象中,也许我的映射错误?
    • 其实映射是对的。您尝试过滤哪个类别值?
    • 您的示例有效,因此我必须验证我的真实数据,感谢您的教程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2021-06-07
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多