阅读 153

es - elasticsearch search - filter

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :filter有什么特点?
答 :
在这里插入图片描述
问 :filter如何使用?
答 :

# filter
PUT /filter_test
{
  "mappings" : {
    "properties" : {
      "brand" : {"type" : "keyword"},
      "color" : {"type" : "keyword"},
      "model" : {"type" : "keyword"}
    }
  }
}

# 索引
POST /filter_test/_doc/1
{
  "brand" : "gucci",
  "color" : "red",
  "model" : "slim"
}

# 索引
POST /filter_test/_doc/2
{
  "brand" : "gucci",
  "color" : "yellow",
  "model" : "slim"
}

# 索引
POST /filter_test/_doc/3
{
  "brand" : "gucci",
  "color" : "green",
  "model" : "slim"
}

# 索引
POST /filter_test/_doc/4
{
  "brand" : "gucci",
  "color" : "blue",
  "model" : "slim"
}

# 索引
POST /filter_test/_doc/5
{
  "brand" : "gucci",
  "color" : "blue",
  "model" : "llim"
}

# 索引
POST /filter_test/_doc/6
{
  "brand" : "gucci",
  "color" : "yellow",
  "model" : "llim"
}


# 索引
POST /filter_test/_doc/7
{
  "brand" : "gucci",
  "color" : "green",
  "model" : "llim"
}

# 索引
POST /filter_test/_doc/8
{
  "brand" : "gucci",
  "color" : "red",
  "model" : "llim"
}

# 搜索 - post filter
# 对聚合没有影响
GET /filter_test/_search
{
  "query" : {
    "bool" : {
      "filter" : [
        {"term" : {"brand" : "gucci"}}
      ]
    }
  },
  "aggs" : {
    "color_aggs" : {
      "terms" : {
        "field" : "color",
        "size"  : 10
      }
    },
    "color_red" : {
      "filter" : {
        "term" : {
          "color" : "red"
        }
      },
      "aggs" : {
          "models" : {
            "terms" : {
              "field" : "model",
              "size"  : 10
            }
          }
      }
    }
  },
  "post_filter" : {
    "term" : {
      "color" : "red"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "8",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "red",
          "model" : "llim"
        }
      },
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "7",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "green",
          "model" : "llim"
        }
      },
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "yellow",
          "model" : "llim"
        }
      },
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "blue",
          "model" : "llim"
        }
      },
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "blue",
          "model" : "slim"
        }
      },
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "green",
          "model" : "slim"
        }
      },
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "yellow",
          "model" : "slim"
        }
      },
      {
        "_index" : "filter_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "brand" : "gucci",
          "color" : "red",
          "model" : "slim"
        }
      }
    ]
  }
}
复制代码
文章分类
后端
文章标签