Elasticsearch习题 3.multi_match&聚合查询

Elasticsearch习题 3.multi_match&聚合查询

Task1

索引task1为食品的配料索引,ingredient字段包含了配料信息, manufacturer字段是对应的供应商信息

  • 请查询配料中包含高锰酸钾的食品数量前三的 的供应商

下为练习所需数据,练习时,可使用拼音替代汉字

POST task1/_bulk
{"index":{"_id":1}}
{"ingredient":"first movie is msb show ","manufacturer":"tengxun"}
{"index":{"_id":2}}
{"ingredient":"first movie is msb show","manufacturer":"tengxun"}
{"index":{"_id":3}}
{"ingredient":"first gaomengsuanjia is msb show","manufacturer":"tengxun"}
{"index":{"_id":4}}
{"ingredient":"first movie is show msb tech","manufacturer":"alibaba"} 
{"index":{"_id":5}}
{"ingredient":"first gaomengsuanjia is msb show ","manufacturer":"alibaba"}
{"index":{"_id":6}}
{"ingredient":"first gaomengsuanjia is msb show","manufacturer":"alibaba"}
{"index":{"_id":7}}
{"ingredient":"first gaomengsuanjia is msb show","manufacturer":"zijie"}
{"index":{"_id":8}}
{"ingredient":"first gaomengsuanjia is show msb tech","manufacturer":"zijie"}
{"index":{"_id":9}}
{"ingredient":"first gaomengsuanjia is msb show ","manufacturer":"zijie"}
{"index":{"_id":10}}
{"ingredient":"first gaomengsuanjia is msb show","manufacturer":"meituan"}
{"index":{"_id":11}}
{"ingredient":"first gaomengsuanjia is msb show","manufacturer":"didi"}
{"index":{"_id":12}}
{"ingredient":"first movie is show msb tech","manufacturer":"meituan"} 

答案


GET task1/_search
{
  "size": 0,
  "query": {
    "match": {
      "ingredient": "gaomengsuanjia"
    }
  }, 
  "aggs": {
    "manufacturer_aggs": {
      "terms": {
        "field": "manufacturer.keyword",
        "order": {
          "_count": "desc"
        },
        "size": 3
      }
    }
  }
}

Task2

创建一个满足一下要求的聚合,测试数据在kibana的home页面添加sample_data的样本测试数据,索引为:kibana_sample_data_flights

  • 统计DistanceKilometers的最大值
  • 在索引中,Carrier为航班,FlightDelayMin为晚点时间,统计不同航班数的晚点信息,并输出最大晚点时间
  • 要求不要返回任何元数据信息
  • 搜索结果只保留request body

答案:

{
 "size": 0,
 "aggs": {
  "distanceKilometers_aggs": {
   "max": {
    "field": "DistanceKilometers"
   }
  },
  "carrier_aggs":{
   "terms": {
    "field": "Carrier"
   },
   "aggs": {
    "info_aggs": {
     "stats": {
      "field": "FlightDelayMin"
     }
    }
   }
  },
  "max_flight_delay_min":{
   "max_bucket": {
    "buckets_path": "carrier_aggs>info_aggs.max"
   }
  }
 }
}

Task3

earthquakes索引中包含了一些地震信息,magiitude为地震等级,pt为地震发生的时间,按下面要求写出聚合脚本。下面包含测试所需数据

难点:每个条件,能想到用哪个聚合函数;函数的嵌套关系。
  • 统计索引数据中每个月的平均地震等级
  • 输出年度月平均地震等级信息(一年之中,平均等级最高月份的震级、最低月份的震级、所有月份平均震级等信息,考试的时候不会有注释,认真题解题意)
  • 输出平均地震等级最高的一个月的平均地震等级
  • 搜索结果不要返回任何元数据
  • 搜索结果需要包含请求头(注意审题)
PUT earthquakes
{
  "mappings": {
    "properties": {
      "pt":{"type":"date"},
      "magiitude":{"type":"integer"}
    }
  }
}

POST earthquakes/_bulk
{"index":{"_id":1}}
{"pt":"2016-01-01T17:00:00", "magiitude":5}
{"index":{"_id":2}}
{"pt":"2016-01-01T20:00:00", "magiitude":5}
{"index":{"_id":3}}
{"pt":"2016-02-01T17:00:00", "magiitude":1}
{"index":{"_id":3}}
{"pt":"2016-02-20T17:00:00", "magiitude":3}
{"index":{"_id":4}}
{"pt":"2016-11-01T17:00:00", "magiitude":5}
{"index":{"_id":5}}
{"pt":"2016-11-01T17:00:00", "magiitude":7}
{"index":{"_id":6}}
{"pt":"2016-11-01T17:00:00", "magiitude":9}
{"index":{"_id":7}}
{"pt":"2016-01-01T17:00:00", "magiitude":8}
{"index":{"_id":8}}
{"pt":"2016-01-01T20:00:00", "magiitude":11}
{"index":{"_id":9}}
{"pt":"2016-02-01T17:00:00", "magiitude":5}
{"index":{"_id":10}}
{"pt":"2016-02-20T17:00:00", "magiitude":6}
{"index":{"_id":11}}
{"pt":"2016-11-01T17:00:00", "magiitude":7}
{"index":{"_id":12}}
{"pt":"2016-11-01T17:00:00", "magiitude":2}
{"index":{"_id":13}}
{"pt":"2016-11-01T17:00:00", "magiitude":3}
{"index":{"_id":14}}
{"pt":"2016-11-01T17:00:00", "magiitude":6}
{"index":{"_id":15}}
{"pt":"2016-11-01T17:00:00", "magiitude":7}

答案:

POST earthquakes/_search
{
  "size": 0,
  "aggs": {
    "month_bucket_agg": {
      "date_histogram": {
        "field": "pt",
        "calendar_interval": "month"
      },
      "aggs": {
        "avg_magiitude": {
          "avg": {
            "field": "magiitude"
          }
        },
        "stats_magiitude": {
          "stats": {
            "field": "magiitude"
          }
        }
      }
    },
    "stats_magiitude_month_stats": {
      "stats_bucket": {
        "buckets_path": "month_bucket_agg>avg_magiitude"
      }
    },
    "max_magiitude_month_stats": {
      "max_bucket": {
        "buckets_path": "month_bucket_agg>stats_magiitude.avg"
      }
    }
  }
}

Task4

对索引task4的三个字段a、b、c查询短语:msb tech, 要求c字段的匹配权重为两倍,要求使用两种方法

答案:


POST task4/_bulk
{"index": {"_id": 1}}
{"a": "xxxx msb","b": "xxxxxx tech xxxxx","c": "xxx msb tech"}
{"index": {"_id": 2}}
{"a": "xxxx aaaaa","b": "xxxxxx msb tech xxxxx","c": "xxx tech"}
{"index": {"_id": 3}}
{"a": "xxxx aaaaa tech ","b": "xxxxxx msb xxxxx","c": "xxx tech  xx  msb"}
{"index": {"_id": 4}}
{"a": "xxxx aaaaa aaa ","b": "xxxxxx msb tech xxxxx","c": "xxx tech  xx  msb"}
{"index": {"_id": 5}}
{"a": "xxxx msb tech ","b": "xxxxxx msb tech xxxxx","c": "xxx  xx  msb"}
{"index": {"_id": 6}}
{"a": "xxxx msb tech ","b": "xxxxxx xxxxx","c": "xxx  xx  msb"}

GET task4/_search
{
  "query": {
    "multi_match": {
      "query": "msb tech",
      "type": "best_fields", 
      "fields": ["a", "b", "c^2"]
    }
  }
}

GET task4/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "a": {
              "query": "msb tech"
            }
          } 
        },
        {
          "match": {
            "b": {
              "query": "msb tech"
            }
          } 
        },
        {
          "match": {
            "c": {
              "query": "msb tech",
              "boost": 2
            }
          } 
        }
      ]
    }
  }
}


本文由 在码圈 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。
本站部分内容收集于互联网,如果有侵权内容、不妥之处,请联系我们删除。敬请谅解!
原文链接:https://www.bedebug.com/archives/elasticsearch-exe3
最后更新于:2022-03-01 18:31:13

请博主喝咖啡 ☕.