Task 1
索引task1
,包含字段title
和field_a
,写出DSL ,要求:
-
要求
title
字段中包含xxx
或者sss
, -
结果先按照
field_a
字段正序,再按照评分倒序 -
title
中高亮匹配结果,并且用b标签嵌套
答案:
PUT task/_bulk
{"index": {"_index": "task1", "_id": 1}}
{"title": "xxx 11111", "fiekld_a": 456}
{"index": {"_index": "task1", "_id": 2}}
{"title": "22222 sss", "fiekld_a": 123}
{"index": {"_index": "task1", "_id": 3}}
{"title": "3333 xxx", "fiekld_a": 789}
{"index": {"_index": "task1", "_id": 4}}
{"title": "zz aaaa", "fiekld_a": 11}
GET task1/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"title": "xxx"
}
},
{
"match": {
"title": "sss"
}
}
]
}
},
"sort": [
{
"fiekld_a": {
"order": "asc"
}
},
{
"_score": {
"order": "desc"
}
}
],
"highlight": {
"fields": {
"title":{
"pre_tags": ["<b>"],
"post_tags": ["</b>"]
}
}
}
}
Task 2
有如下索引
POST inject/_doc/1
{
"title": "the number is error"
}
POST inject/_doc/2
{
"title": "it is number is error"
}
用下面的查询是返回一条数据
GET inject/_search
{
"query": {
"match": {
"title": "the"
}
}
}
要求:
-
新建一个索引 named inject_new
-
通过
Reindex API
将inject
的数据和类型复制到inject_new
上 -
通过 term查询,term 'the' value of title,返回0条数据。
提示
- 使用
_reindex
API可以重建索引
答案:
PUT inject_new
{
"mappings": {
"properties": {
"title":{
"type": "text",
"analyzer": "stop",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
POST _reindex
{
"source": {
"index": "inject"
},
"dest": {
"index": "inject_new"
}
}
GET inject_new/_search
{
"query": {
"term": {
"title": "number"
}
}
}
Task3
索引中index_a
有两条分别是waynes
和wayne's
的数据,要求:
-
将
index_a
通过reindex
到index_b
, -
对于index_b,使用waynes或wayne's的查询,能返回同样数量的文档和评分
答案:
POST index_a/_bulk
{"index": {"_id": "1"}}
{"title": "xxxx waynes ew12"}
{"index": {"_id": "2"}}
{"title": "it is wayne's"}
GET index_a/_mapping
PUT index_b
{
"settings": {
"analysis": {
"char_filter": {
"my_char_filter":{
"type": "mapping",
"mappings": [
"wayne's => waynes"
]
}
},
"analyzer": {
"my_analyer": {
"type": "custom",
"tokenizer": "standard",
"char_filter": ["my_char_filter"],
"filter": []
}
}
}
},
"mappings" : {
"properties" : {
"title" : {
"type" : "text",
"analyzer": "my_analyer",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
POST _reindex
{
"source": {
"index": "index_a"
},
"dest": {
"index": "index_b"
}
}
GET index_b/_search
{
"query": {
"match": {
"title": "waynes"
}
}
}
GET index_b/_search
{
"query": {
"match": {
"title": "wayne's"
}
}
}