ElasticSearch 学习 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MaoRong
0.95D
0.23D
V2EX    Elasticsearch

ElasticSearch 学习

  •  
  •   MaoRong 2021-08-18 19:43:42 +08:00 2392 次点击
    这是一个创建于 1514 天前的主题,其中的信息可能已经有所发展或是发生改变。

    es 大致结构

    { "track_total_hits": true, //获取 query 里面查询到的总条数 "query":{},//查询语句筛选条件 "aggs":{},//管道 "sort":[],//排序 "_source":[],//字段筛选 "size":0,//查询的大小 "from":0//从哪里查 } 

    关于 query

    match_phrase 与 match

    match_phrased 必须要包含查询的词组,math 可以包含查询的词组也可以将词组拆分查询

    来源:https://blog.csdn.net/liuxiao723846/article/details/78365078

    terms 与 term

    term (精确值查找)

    term 查询, 可以用它处理数字( numbers )、布尔值( Booleans )、日期( dates )以及文本( text )

    trems (查找多个精确值)

    terms 是 包含( contains ) 操作,而非 等值( equals )

    其中 country_number_id 需要使用.keyword

    { "terms": { "country_number_id.keyword": [ "CN", "HK", "IN", "JP", "KR", "MO" ] } } 

    重点 aggs

    返回数据 是从 aggregations 中获取

    示例:

    "aggs": { "country_number_id": { "terms": { "field": "country_number_id.keyword", "size": 10000 //这里指的是需要返回 country_number_id 的桶的个数,但不代表是查询的数据量 }, "aggs": { "category_number_id": { "terms": { "field": "category_number_id.keyword", "size": 10000,//同上,不过是 category_number_id "order": { "revenue": "desc"//这个字段用的是下面 sum 聚合的 revenue } }, "aggs": { "revenue": { "sum": {//sum,avg,min,max "field": "revenue" } }, "downloads": { "sum": { "field": "downloads" } }, "top": {//随便取名 "top_hits": {//关键字 "size": 1,//category_number_id 下面桶里数据的条数 "_source": [//里面命中数据字段筛选 "os", "revenue", "country_number_id", "category_number_id", "category_name", "country_name", "downloads" ] } } } } } } 

    关于 aggs 的疑惑

    1. 多个字段聚合的时候怎么做排序,比如这边用 country_number_id 和 category_number_id 聚合,使用 sum 来的 revenue 做一个排序,但是实际是 country_number_id 大桶里面包含小桶,小桶里面可以根据 revenue 排序,无法做一整个排序
    2. 多个字段聚合的时候将两个字段嵌套顺序替换,查出来的数据量变了,其他筛选条件什么都没变
    3 条回复    2021-08-19 14:09:20 +08:00
    BJL
        1
    BJL  
       2021-08-19 11:26:09 +08:00
    1.后续代码处理
    2.如果你说的是上面的 country 和 category,可能的原因有一个,terms 聚合在下面这种情况会有误差
    This means that if the number of unique terms is greater than size, the returned list is slightly off and not accurate (it could be that the term counts are slightly offand it could even be that a term that should have been in the top size buckets was not returned).
    BJL
        2
    BJL  
       2021-08-19 11:54:44 +08:00
    当然,1 这个排序问题也可以在 country 下一级,category 平级再加一个对 revenue 的 sum agg,让 coutry 的 order 按这个新加的来
    MaoRong
        3
    MaoRong  
    OP
       2021-08-19 14:09:20 +08:00
    @BJL 多谢,我去试试
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     6094 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 02:30 PVG 10:30 LAX 19:30 JFK 22:30
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86