elasticsearch nested polymerization drill analysis, analysis polymerization

The fielddata property of the text field is true, to polymerization

PUT /ecommerce/_mapping/product
 {       
  "properties": {
   "tags": {  
             "type": "text",
            "fielddata": true
         }       
    }         
 }

Analysis of the polymerization, count the number of items in each tag

GET /ecommerce/product/_search
{
  "size": 0, 
  "aggs":{
    "group_by_tags":{
      "terms": {
        "field": "tags"

      }
    }
  }
}

Commodity name contains yagao calculates the number of items in each tag


GET /ecommerce/product/_search
{
  "size": 0, 
  "query": {
    "match": {
      "name": "yagao"
    }

  }, 
  "aggs":{
    "group_by_tags":{
      "terms": {
        "field": "tags"

      }
    }
  }
}

The first packet, the average value calculated for each group and then calculates the average price of the commodity at each tag

GET /ecommerce/product/_search
{
  "size": 0, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags"
      },
      "aggs": {
        "arg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

The average price of each commodity calculated in the tag, and sorted in descending order of average price


GET /ecommerce/product/_search
{
  "size": 0, 
  "aggs": {
    "group_by_tags": {
      "terms": {
        "field": "tags",
        "order": {
          "arg_price": "desc"
        }
      },
      "aggs": {
        "arg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

Interval specified price range in accordance with the packet, then grouped by tag within each group, and finally calculate the average price for each group, the two drill


GET /ecommerce/product/_search
{
  "size": 0,
  "aggs": {
    "group_by_price": {
      "range": {
        "field": "price",
        "ranges": [
          {
            "from": 0,
            "to": 20
          },
           {
            "from": 20,
            "to": 40
          },

           {
            "from": 40,
            "to": 60
          }
        ]
      },
      "aggs": {
        "group_by_tags": {
          "terms": {
            "field": "tags"
          },
          "aggs": {
            "avg_price": {
             "avg": {
               "field": "price"
             }
            }
          }
        }
      }
    }
  }

}

Guess you like

Origin blog.51cto.com/395469372/2412343