Search Template
Search Template可以用于程序后端层面和es后端层面解耦
DELETE _scripts/tmdb
GET _scripts/tmdb
#定义1个_script
POST _scripts/tmdb
{
"script": {
"lang": "mustache",
"source": {
"_source": [
"title","overview"
],
"size": 20,
"query": {
"multi_match": {
"query": "{{q}}",
"fields": ["title","overview"]
}
}
}
}
}
POST tmdb/_bulk
{ "index": { "_id": 1 }}
{ "title" :"title1", "overview" : "overview1","level":1 }
{ "index": { "_id": 2 }}
{ "title" :"title2", "overview" : "overview2","level":2 }
{ "index": { "_id": 3 }}
{ "title" :"title3", "overview" : "overview3" ,"level":3}
GET tmdb/_search
{}
#通过id引用_script的id,从而使用一个Search Template
POST tmdb/_search/template
{
"id":"tmdb",
"params": {
"q": "title2 overview3"
}
}
结果如下:
"max_score" : 0.9808291,
"hits" : [
{
"_index" : "tmdb",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.9808291,
"_source" : {
"overview" : "overview2",
"title" : "title2"
}
},
{
"_index" : "tmdb",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.9808291,
"_source" : {
"overview" : "overview3",
"title" : "title3"
}
}
]
Index Alas查询
新增索引别名
POST _aliases
{
"actions": [
{
"add": {
"index": "tmdb",
"alias": "tmdb-latest"
}
}
]
}
根据索引别名搜索
POST tmdb-latest/_search
{
"query": {
"match_all": {}
}
}
结果:
"max_score" : 1.0,
"hits" : [
{
"_index" : "tmdb",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "title2",
"overview" : "overview2",
"level" : 2
}
},
{
"_index" : "tmdb",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"title" : "title3",
"overview" : "overview3",
"level" : 3
}
}
]
新建索引别名,并制定条件
POST _aliases
{
"actions": [
{
"add": {
"index": "tmdb",
"alias": "tmdb-latest-highlevel",
"filter": {
"range": {
"level": {
"gte": 2
}
}
}
}
}
]
}
根据索引别名搜索
POST tmdb-latest-highlevel/_search
{
"query": {
"match_all": {}
}
}
结果如下:
"max_score" : 1.0,
"hits" : [
{
"_index" : "tmdb",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"title" : "title2",
"overview" : "overview2",
"level" : 2
}
},
{
"_index" : "tmdb",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"title" : "title3",
"overview" : "overview3",
"level" : 3
}
}
]
版权声明:本文为accordall原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。