群日常问题
动态字段的问题nested?
经测试不支持list<map> 和 list<? extend map>
支持指定字段在es的索引中忽略大小写,以便在term查询时不区分大小写,仅对keyword类型字段生效,es的规则,并非框架限制.
Normalizers是Elasticsearch中用于文本规范化的一种组件,它们通常用于关键字字段(keyword fields),这些字段需要规范化但不需要分词。Normalizers和analyzers类似,但只能包含字符过滤器(char_filter)和标记过滤器(token filter),而不包含分词器(tokenizer)。
PUT test0612
# 关闭后只能读
POST test0612/_close
PUT /test0612/_settings
{
"index":{
"analysis":{
"normalizer":{
"lowercase_normalizer":{
"type": "custom",
"char_filter": [],
"filter": ["lowercase"]
}
}
}
}
}
POST test0612/_open
PUT /test0612/_mapping
{
"properties": {
"name": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
},
"preName": {
"type": "keyword"
}
}
}
POST _analyze
{
"text": "TEST测试",
"analyzer": "ik_max_word"
}
GET test0612/_analyze
{
"field": "name",
"text": "TEST测试"
}
GET test0612/_analyze
{
"field": "preName",
"text": "TEST测试"
}
测试
PUT test0612/_doc/1
{
"id": "1",
"name": "Mohammed Ahmed",
"preName": "Mohammed Ahmed"
}
PUT test0612/_doc/2
{
"id": "2",
"name": "Sophie Martin",
"preName": "Mohammed Ahmed"
}
PUT test0612/_doc/3
{
"id": "3",
"name": "mohammed ahmed",
"preName": "Mohammed Ahmed"
}
GET test0612/_search
{
"query": {
"wildcard": {
"name": {
"value": "*moham*"
}
}
}
}
GET test0612/_search
{
"query": {
"term": {
"name": {
"value": "MohAmmed Ahmed"
}
}
}
}
join属于文档间建立父子关系,若一个索引中通过某个字段建立的父子关系,该如何使用呢?
Nested
首先要说nested,为什么要nested, 如果不指定nested, 那么默认的类型是Object, 其內部结构是平铺类型,无法排序和聚合,
默认情况下的平铺查询方式
# 平铺查询会导致查询到非张四的数据
GET /test_0611/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"users.name": "张"
}
},
{
"match": {
"users.name": "四"
}
}
]
}
}
}
嵌套
POST /test_0611/_mapping
{
"properties": {
"users": {
"type": "nested"
}
}
}
POST /test_0611/_doc
{
"users": [
{
"name": "张 三",
"age": 23,
"email": "5@qq.com"
},
{
"name": "李 四",
"age": 18,
"email": "6@qq.com"
}
]
}
# 查询不到数据
GET /test_0611/_search
{
"query": {
"nested": {
"path": "users",
"query": {
"bool": {
"must": [
{
"match": {
"users.name": {
"query": "张",
"operator": "and"
}
}
},
{
"match": {
"users.name": {
"query": "四",
"operator": "and"
}
}
}
]
}
}
}
}
}
JOIN
DELETE my_blogs
PUT my_blogs
{
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"blog_comment_relation": {
"type": "join",
"relations": {
"blog": "comment"
}
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"title": {
"type": "keyword"
}
}
}
}
#! name 标记为父文档, 指定id = blog1
PUT my_blogs/_doc/blog1
{
"title": "中间人还有未来吗?",
"content": "中间人还有未来吗?我觉的应该有",
"blog_comment_relation": {
"name": "blog"
}
}
#! 由es生成Id; YiBtCpAB3lg0W2IdEzLJ
POST my_blogs/_doc
{
"title": "宇宙的尽头是什么?",
"content": "或许是灵魂在银河里畅游",
"blog_comment_relation": {
"name": "blog"
}
}
#! 指定id=comment1
PUT my_blogs/_doc/comment1?routing=blog1
{
"title": "精彩",
"content": "你的文章很有意思",
"user":"王二小",
"blog_comment_relation": {
"name": "comment",
"parent": "blog1"
}
}
# 不指定Id
POST my_blogs/_doc?routing=YiBtCpAB3lg0W2IdEzLJ
{
"title": "不同意",
"content": "宇宙的尽头应该还是宇宙",
"user":"张三",
"blog_comment_relation": {
"name": "comment",
"parent": "YiBtCpAB3lg0W2IdEzLJ"
}
}
设计父子结构时要考虑将父子的所有字段能考虑进去
# 父和子的所有字段
GET /my_blogs/_mapping
# 父和子的所有文档
GET /my_blogs/_search
{
"query": {"match_all": {}}
}
# 父文档查询, 不会包含子文档
GET my_blogs/_doc/blog1
# 通过parent_id查询子文档
POST my_blogs/_search
{
"query": {
"parent_id": {
"type": "comment",
"id": "blog1"
}
}
}
# 使用has_child,通过child查询parent
# 要求有孩子类型comment , 并且孩子 content match 文章
POST my_blogs/_search
{
"query": {
"has_child": {
"type": "comment",
"query": {
"match": {
"content": "文章"
}
}
}
}
}
# 使用has_parent,查询关联的子文档
POST my_blogs/_search
{
"query": {
"has_parent": {
"parent_type": "blog",
"query": {
"match": {
"content": "中间"
}
}
}
}
}
# 不允许根据子ID查询
GET my_blogs/_doc/comment1
这样是可以的
GET my_blogs/_doc/comment1?routing=blog1
ElsticSearch常用指南
最新推荐文章于 2025-05-20 11:57:34 发布