Silentdoer

导航

ElasticSearch随记

最新的ES的index type默认就是_doc无法再指定;

index里的每个文档都有个String类型的_id字段,它可以理解为主键【所以表结构最好是有个单字段且业务无关作为主键方便迁移ES】

keyword的字段才能被聚合和精确查找

es默认有from+size不能超过1W的限制,但是可以用scroll的方式来实现下载全部数据;

es支持正则表达式(但是#字段必须要转义),支持like这种和null(字段没有传值)和not xxx

range里的gte等是可以被from和to代替的,但是同时会用include_lower这种来代表是<=还是<

ES支持or的方式,should【最外面是bool,然后是must,must里的每个元素都是and关系,但是元素又可以是bool,这个bool内部用should,则should里的元素就变成了括号or的关系】,大致为:

/*name == "a" and (city == "b" or city == "c")*/
{
    "query":{
    "bool": {
        "must": [
            {
                "match_phrase": {
                    "name": "a"
                }
            },
            {
                "bool": {
                    "should": [
                        {
                            "match_phrase": {
                                "city": "b"
                            }
                        },
                        {
                            "match_phrase": {
                                "city": "c"
                            }
                        }
                    ]
                }
            }
       ]
    }
    }
}

 

通过这种方式可以先按条件查询出来,然后再根据prop1和prop2两个列进行group by,group by的key类似 prop1Value,prop2Value

{
	"size": 0,
	"query": {
		"match_phrase": {
			"username": "aaa"
		}
	},
	"aggregations": {
		"result": { /*result是自定义的名字,对应java代码里的AggregationBuilders.terms("result").script(script);*/
			"terms": {
				"script": {
					"source": "doc['prop1'].value + ',' + doc['prop2'].value",
					"lang": "painless"
				}
			}
		}
	}
}
/*其结果大致为*/
{
    ...
    "aggregations": {
        "result": {
            ...
            "buckets": [
                {
                    "key": "prop1Value,prop2Value",
                    "doc_count": 3
                }
            ]
        }
    }
}

 

posted on 2022-06-21 19:58  Silentdoer  阅读(22)  评论(0编辑  收藏  举报