一、题目
在集群上有两个索引 index_a 和 index_b,index_a 中有字段 field_a,没有 field_a_desc;index_b 中有 field_a、field_a_desc。其中 index_b 中共有 10 条数据。
要求:
reindex
一个索引 index_c,要求包含 index_a 中的所有数据- index_c 中新增 field_a_desc 字段,且 field_a 的值与 index_b 索引中 field_a 的值相同。
# 创建索引结构
PUT index_a
{
"mappings": {
"properties": {
"field_a":{
"type":"keyword"
},
"field_a-01":{
"type": "keyword"
}
}
}
}
PUT index_b
{
"mappings": {
"properties": {
"field_a":{
"type": "keyword"
},
"field_a_desc":{
"type": "keyword"
}
}
}
}
# 索引数据导入
POST index_a/_bulk
{"index":{"_id":1}}
{"field_a":"a001","field_a_01":"usera01"}
{"index":{"_id":2}}
{"field_a":"a002","field_a_01":"usera02"}
{"index":{"_id":3}}
{"field_a":"a003","field_a_01":"usera03"}
{"index":{"_id":4}}
{"field_a":"a001","field_a_01":"usera011"}
{"index":{"_id":5}}
{"field_a":"a002","field_a_01":"usera012"}
{"index":{"_id":6}}
{"field_a":"a003","field_a_01":"usera013"}
{"index":{"_id":7}}
{"field_a":"a007","field_a_01":"usera021"}
{"index":{"_id":8}}
{"field_a":"a008","field_a_01":"usera022"}
{"index":{"_id":9}}
{"field_a":"a009","field_a_01":"usera023"}
{"index":{"_id":10}}
{"field_a":"a010","field_a_01":"usera031"}
{"index":{"_id":11}}
{"field_a":"a008","field_a_01":"usera032"}
{"index":{"_id":12}}
{"field_a":"a009","field_a_01":"usera033"}
POST index_b/_bulk
{"index":{"_id":1}}
{"field_a":"a001","field_a_desc":"money_01"}
{"index":{"_id":2}}
{"field_a":"a002","field_a_desc":"money_02"}
{"index":{"_id":3}}
{"field_a":"a003","field_a_desc":"money_03"}
{"index":{"_id":4}}
{"field_a":"a004","field_a_desc":"money_04"}
{"index":{"_id":5}}
{"field_a":"a005","field_a_desc":"money_05"}
{"index":{"_id":6}}
{"field_a":"a006","field_a_desc":"money_06"}
{"index":{"_id":7}}
{"field_a":"a007","field_a_desc":"money_07"}
{"index":{"_id":8}}
{"field_a":"a008","field_a_desc":"money_08"}
{"index":{"_id":9}}
{"field_a":"a009","field_a_desc":"money_09"}
{"index":{"_id":10}}
{"field_a":"a010","field_a_desc":"money_10"}
1.1 考点
1.2 答案
# 新建enrich 策略
PUT /_enrich/policy/my-policy
{
"match": {
"indices": "index_b",
"match_field": "field_a",
"enrich_fields": ["field_a_desc"]
}
}
# 执行策略
POST /_enrich/policy/my-policy/_execute
# 建立管道
PUT /_ingest/pipeline/my_pipeline
{
"processors" : [
{
"enrich" : {
"policy_name": "my-policy",
"field" : "field_a",
"target_field": "add_field",
"max_matches": "1"
}
},
{
"rename": {
"field": "add_field.field_a_desc",
"target_field": "field_a_desc",
"ignore_failure": true
}
},
{
"remove": {
"field": "add_field",
"ignore_failure": true
}
}
]
}
# 重建索引
POST _reindex
{
"source": {
"index": "index_a"
},
"dest": {
"index": "index_c",
"pipeline": "my_pipeline"
}
}
# 查询结果
GET index_c/_search
{
"query": {
"match_all": {}
}
}
二、题目
在集群上有一个索引 task3,搜索满足以下要求:
- 字段
a
,b
,c
三个字段任意一个字段,可以搜到文本 cake mix highlight
字段d
,加标签sort
字段e
正序,_score
降序
PUT task3
{
"mappings": {
"properties": {
"a":{
"type": "text"
},
"b":{
"type": "text"
},
"c":{
"type": "text"
},
"d":{
"type":"text"
},
"e":{
"type": "keyword"
}
}
}
}
POST task3/_bulk
{"index": {}}
{"a":"cake mix", "b":"a", "c":"a", "d":"cake mix", "e":"1"}
{"index": {}}
{"a":"b", "b":"b", "c":"b", "d":"cake mix", "e":"2"}
{"index": {}}
{"a":"c", "b":"c", "c":"cake mix", "d":"cake mix", "e":"3"}
2.1 考点
2.2 答案
GET task3/_search
{
"query": {
"multi_match": {
"query": "cake mix",
"fields": ["a", "b", "c"]
}
},
"highlight": {
"pre_tags" : ["<tag1>"],
"post_tags" : ["</tag1>"],
"fields" : {
"d": {}
}
},
"sort": [
{
"e": {
"order": "asc"
}
},
{
"_score": {
"order": "desc"
}
}
]
}
24-06-30 补充一个 bool
的解法吧
GET task3/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"a": "cake mix"
}
},
{
"match": {
"b": "cake mix"
}
},
{
"match": {
"c": "cake mix"
}
}
],
"minimum_should_match": 1
}
},
"highlight": {
"fields": {
"d" : { "pre_tags" : ["<em>"], "post_tags" : ["</em>"] }
}
},
"sort": [
{
"e": {
"order": "asc"
}
},
{
"_score": {
"order": "desc"
}
}
]
}