官术网_书友最值得收藏!

Implementation of filters in Elasticsearch

This section sheds some light on the implementation of filters in Elasticsearch. All filters are not the same. We will see how each one differs from another and how we can implement these with Elasticsearch. First, let's see all the three implementation types of filters in Elasticsearch.

We have already showed you the most basic type of usage of a filter in the previous examples. You would have noticed the term constant score or filtered every time we used a filter. These are queries and they wrap the filter inside and apply it against the normal query's result. This is the first type of filter implementation of Elasticsearch.

Note that the filtered/constant score implementation of filters will affect both the results of the query and aggregations; some of the examples are shown as follows:

curl -XPOST 'http://localhost:9200/products/_search?pretty' -d '{
   "aggregations": {
     "department": {
       "terms": {
         "field": "color"
       }
     }
   },
   "query": {
     "filtered": {
       "query": {
         "match": {
           "description": "dell"
         }
       },
       "filter": {
         "term": {
           "productType": "Laptop"
         }
       }
     }
   }
 }'

The output of the preceding query is as follows:

{
  "took" : 250,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.48288077,
    "hits" : [ {
      "_index" : "products",
      "_type" : "product",
      "_id" : "-B0YVCS6R1iXOqhHNKRcJQ",
      "_score" : 0.48288077,
      "_source":{    "name" : "Dell Vostro 3546 Laptop", "description" : "Dell Vostro 3546 Laptop with Laptop Bag", "dateOfManufactoring" : "2013-05-06",    "prize" : 25688, "totalBuy" : 20 ,    "color" : "Yellow",    "productType" : "Laptop",    "imageURL" : "www.imageDB.com/urlToDellVostroLaptop.jpg"}
    }, {
      "_index" : "products",
      "_type" : "product",
      "_id" : "J-4lWEDDQPaondNpQtbFLQ",
      "_score" : 0.40240064,
      "_source":{    "name" : "Dell Inspiron Laptop", "description" : "Dell Inspiron 3541 Black 15.6 inch Laptop without Laptop Bag",    "dateOfManufactoring" : "2012-04-01",    "prize" : 19990,    "totalBuy" : 334 , "color" : "Grey",    "productType" : "Laptop",    "imageURL" : "www.imageDB.com/urlToDellInspironLaptop.jpg"}
    } ]
  },
  "aggregations" : {
    "department" : {
      "buckets" : [ {
        "key" : "Grey",
        "doc_count" : 1
      }, {
        "key" : "Yellow",
        "doc_count" : 1
      } ]
    }
  }
}
主站蜘蛛池模板: 彩票| 思茅市| 西乌珠穆沁旗| 普兰县| 如皋市| 松阳县| 泰安市| 巴东县| 琼结县| 夹江县| 垫江县| 石台县| 淮滨县| 武定县| 葵青区| 普兰店市| 尉氏县| 益阳市| 峨眉山市| 奇台县| 迁西县| 北京市| 普宁市| 甘孜| 贺兰县| 开封市| 太湖县| 沙田区| 荥阳市| 闵行区| 泾源县| 南阳市| 化德县| 上林县| 仁寿县| 林口县| 青铜峡市| 云安县| 兴义市| 阿克陶县| 吉木乃县|