首页 > 其他分享 >elasticsearch之search template

elasticsearch之search template

时间:2023-01-19 08:55:15浏览次数:47  
标签:search name score elasticsearch template test match

一、search template简介

elasticsearch提供了search template功能,其会在实际执行查询之前,对search template进行预处理并将参数填充到template中。

elasticsearch主要提供了两个API来支持search template
_scripts/用于对search template的维护;
_search/template使用search template进行搜索;

二、测试数据准备

批量index三个文档

POST _bulk

{ "index" : { "_index" : "search_template_test", "_type" : "_doc", "_id" : "1" } }
{ "name":"zhang san",	"age":30,	"man":true,	"address":"Hebei LangFang"}
{ "index" : { "_index" : "search_template_test", "_type" : "_doc", "_id" : "2" } }
{ "name":"li si",	"age":20,	"man":true,	"address":"BeiJing HaiDian"}
{ "index" : { "_index" : "search_template_test", "_type" : "_doc", "_id" : "3" } }
{ "name":"liu hui",	"age":40,	"man":false,	"address":"NeiMengGu ChiFeng"}


查询查看三个索引的文档

GET search_template_test/_search
{
    "took":6,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":3,
        "max_score":1,
        "hits":[
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"2",
                "_score":1,
                "_source":{
                    "name":"li si",
                    "age":20,
                    "man":true,
                    "address":"BeiJing HaiDian"
                }
            },
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"1",
                "_score":1,
                "_source":{
                    "name":"zhang san",
                    "age":30,
                    "man":true,
                    "address":"Hebei LangFang"
                }
            },
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"3",
                "_score":1,
                "_source":{
                    "name":"liu hui",
                    "age":40,
                    "man":false,
                    "address":"NeiMengGu ChiFeng"
                }
            }
        ]
    }
}

三、维护search template

我们新建id为search_template_test.match_name的search template,其主要是查询match 字段name;

POST _scripts/search_template_test.match_name
{
    "script": {        
        "lang": "mustache",
        "source": {
            "query": {
                "match": {
                    "name": "{{name_val}}"
                }
            }
        }
    }
}

查看我们创建的search template

GET _scripts/search_template_test.match_name

{
    "_id":"search_template_test.match_name",
    "found":true,
    "script":{
        "lang":"mustache",
        "source":"{\"query\":{\"match\":{\"name\":\"{{name_val}}\"}}}",
        "options":{
            "content_type":"application/json; charset=UTF-8"
        }
    }
}

删除我们创建的search template

DELET _scripts/search_template_test.match_name
{
    "acknowledged":true
}

elasticsearch提供了API支持我们查看预处理最终形成的查询语句;

POST _render/template/search_template_test.match_name
{
    "params":{
        "name_val":"zhang li"
    }
}


{
    "template_output":{
        "query":{
            "match":{
                "name":"zhang li"
            }
        }
    }
}

四、使用search template

我们可以像下边这样使用search template

POST search_template_test/_search/template
{
    "id": "search_template_test.match_name", 
    "params": {
        "name_val":"zhang li"
    }
}



{
    "took":1,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":2,
        "max_score":0.2876821,
        "hits":[
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"2",
                "_score":0.2876821,
                "_source":{
                    "name":"li si",
                    "age":20,
                    "man":true,
                    "address":"BeiJing HaiDian"
                }
            },
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"1",
                "_score":0.2876821,
                "_source":{
                    "name":"zhang san",
                    "age":30,
                    "man":true,
                    "address":"Hebei LangFang"
                }
            }
        ]
    }
}

执行search template也支持使用explain来调试查看elasticsearch的打分情况;

POST search_template_test/_search/template
{
    "id": "search_template_test.match_name", 
    "params": {
        "name_val":"zhang li"
    },
    "explain":true
}




{
    "took":3,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":2,
        "max_score":0.2876821,
        "hits":[
            {
                "_shard":"[search_template_test][2]",
                "_node":"Sl6S4Kn2Rh-BdNgaM3ZwJg",
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"2",
                "_score":0.2876821,
                "_source":{
                    "name":"li si",
                    "age":20,
                    "man":true,
                    "address":"BeiJing HaiDian"
                },
                "_explanation":{
                    "value":0.2876821,
                    "description":"sum of:",
                    "details":[
                        {
                            "value":0.2876821,
                            "description":"weight(name:li in 0) [PerFieldSimilarity], result of:",
                            "details":[
                                ......
                            ]
                        }
                    ]
                }
            },
            {
                "_shard":"[search_template_test][3]",
                "_node":"Sl6S4Kn2Rh-BdNgaM3ZwJg",
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"1",
                "_score":0.2876821,
                "_source":{
                    "name":"zhang san",
                    "age":30,
                    "man":true,
                    "address":"Hebei LangFang"
                },
                "_explanation":{
                    "value":0.2876821,
                    "description":"sum of:",
                    "details":[
                        {
                            "value":0.2876821,
                            "description":"weight(name:zhang in 0) [PerFieldSimilarity], result of:",
                            "details":[
                                ......
                            ]
                        }
                    ]
                }
            }
        ]
    }
}

执行search template也支持使用profile来调试查看elasticsearch的查询执行情况;

POST search_template_test/_search/template
{
    "id": "search_template_test.match_name", 
    "params": {
        "name_val":"zhang li"
    },
    "profile":true
}



{
    "took":11,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":2,
        "max_score":0.2876821,
        "hits":[

        ]
    },
    "profile":{
        "shards":[
            {
                "id":"[Sl6S4Kn2Rh-BdNgaM3ZwJg][search_template_test][0]",
                "searches":[
                    {
                        "query":[
                            {
                                "type":"BooleanQuery",
                                "description":"name:zhang name:li",
                                "time_in_nanos":308301,
                                "breakdown":{
                                    "score":0,
                                    "build_scorer_count":0,
                                    "match_count":0,
                                    "create_weight":308300,
                                    "next_doc":0,
                                    "match":0,
                                    "create_weight_count":1,
                                    "next_doc_count":0,
                                    "score_count":0,
                                    "build_scorer":0,
                                    "advance":0,
                                    "advance_count":0
                                },
                                "children":[
                                    {
                                        "type":"TermQuery",
                                        "description":"name:zhang",
                                        "time_in_nanos":32901,
                                        "breakdown":{
                                            "score":0,
                                            "build_scorer_count":0,
                                            "match_count":0,
                                            "create_weight":32900,
                                            "next_doc":0,
                                            "match":0,
                                            "create_weight_count":1,
                                            "next_doc_count":0,
                                            "score_count":0,
                                            "build_scorer":0,
                                            "advance":0,
                                            "advance_count":0
                                        }
                                    },
                                    {
                                        "type":"TermQuery",
                                        "description":"name:li",
                                        "time_in_nanos":15901,
                                        "breakdown":{
                                            "score":0,
                                            "build_scorer_count":0,
                                            "match_count":0,
                                            "create_weight":15900,
                                            "next_doc":0,
                                            "match":0,
                                            "create_weight_count":1,
                                            "next_doc_count":0,
                                            "score_count":0,
                                            "build_scorer":0,
                                            "advance":0,
                                            "advance_count":0
                                        }
                                    }
                                ]
                            }
                        ],
                        "rewrite_time":42700,
                        "collector":[
                            {
                                "name":"CancellableCollector",
                                "reason":"search_cancelled",
                                "time_in_nanos":700,
                                "children":[
                                    {
                                        "name":"SimpleTopScoreDocCollector",
                                        "reason":"search_top_hits",
                                        "time_in_nanos":200
                                    }
                                ]
                            }
                        ]
                    }
                ],
                "aggregations":[

                ]
            }
        ]
    }
}

五、使用search template处理复杂的查询

我们构建以下search template,其功能如下

可以通过name字段进行match,如果命中字段中的各个关键字相邻则有更高的权重打分,同时可以通过address字段进行模糊匹配,这三个查询只要命中一个即可;

man字段值必须符合输入的值;

POST _scripts/search_template_test.multi_search_template
{
    "script":{
        "lang":"mustache",
        "source":{
            "query":{
                "bool":{
                    "should":[
                        {
                            "match":{
                                "name":"{{name}}"
                            }
                        },
                        {
                            "match_phrase_prefix":{
                                "name":"{{name}}"
                            }
                        },
                        {
                            "query_string":{
                                "default_field":"address",
                                "query":"{{#join delimiter=' '}}address{{/join delimiter=' '}}"
                            }
                        }
                    ],
                    "filter":{
                        "term":{
                            "man":"{{man}}"
                        }
                    },
                    "minimum_should_match":1
                }
            }
        }
    }
}

通过以下参数命中前两条数据

POST search_template_test/_search/template
{
    "id":"search_template_test.multi_search_template",
    "params":{
        "name":"zhang sa",
        "address":[
            "*bei*",
            "*chi*"
        ],
        "man":true
    }
}


{
    "took":10,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":2,
        "max_score":1.8630463,
        "hits":[
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"1",
                "_score":1.8630463,
                "_source":{
                    "name":"zhang san",
                    "age":30,
                    "man":true,
                    "address":"Hebei LangFang"
                }
            },
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"2",
                "_score":1,
                "_source":{
                    "name":"li si",
                    "age":20,
                    "man":true,
                    "address":"BeiJing HaiDian"
                }
            }
        ]
    }
}

我们将man字段设置为false,从而只匹配第三条记录;

POST search_template_test/_search/template
{
    "id":"search_template_test.multi_search_template",
    "params":{
        "name":"zhang sa",
        "address":[
            "*bei*",
            "*chi*"
        ],
        "man":false
    }
}

{
    "took":4,
    "timed_out":false,
    "_shards":{
        "total":5,
        "successful":5,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":1,
        "max_score":1,
        "hits":[
            {
                "_index":"search_template_test",
                "_type":"_doc",
                "_id":"3",
                "_score":1,
                "_source":{
                    "name":"liu hui",
                    "age":40,
                    "man":false,
                    "address":"NeiMengGu ChiFeng"
                }
            }
        ]
    }
}

标签:search,name,score,elasticsearch,template,test,match
From: https://www.cnblogs.com/wufengtinghai/p/17061029.html

相关文章

  • 34-ElasticSearch-初步检索的基本操作
    1、_cat用PostMan测试如下2、索引一个文档(保存)Post如果不指定id,会自动生成id,并且每次执行都是新增操作。如果带上id就是修改,并且新增版本号Put可以新增也可以修改。......
  • 学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API
    2023-01-18一、定义切面优先级  1、语法:@Order(value=index)①index是int类型,默认值是int可存储的最大值②数值越小,优先级越高二、Spring中的JdbcTemplate1、JdbcT......
  • RestTemplate去掉https验证
    /*@BeanpublicRestTemplaterestTemplate()throwsKeyStoreException,NoSuchAlgorithmException,KeyManagementException{TrustStrategyacceptingTrustSt......
  • Elasticsearch 技术分析(一): 基础入门
    简介Elasticsearch是一个高度可扩展的、开源的、基于Lucene的全文搜索和分析引擎。它允许您快速,近实时地存储,搜索和分析大量数据,并支持多租户。Elasticsearch也使用Ja......
  • Line search and Step length线搜索与步长
    ​​WelcomeToMyBlog​​​在最优化(optimization)问题中,线搜索(linesearch)和置信域(trustregion)方法是寻找局部最小值(localminimum)基本迭代方法(iterativeap......
  • 技术实践|浅谈Elasticsearch跨集群搜索
    前言Elasticsearch(简称ES)是基于Lucene库的分布式架构搜索引擎。它支持水平横向扩展,但是集群节点不能无限增加。因为当集群的meta信息(节点、索引、集群状态)过多时,会使集群更......
  • ElasticSearch进阶:一文全览各种ES查询在Java中的实现
    1词条查询1.1等值查询-term1.2多值查询-terms1.3范围查询-range1.4前缀查询-prefix1.5通配符查询-wildcard2复合查询2.1布尔查询2.2Filter查询3聚......
  • ElasticSearch7.10配置Search-Guard之配置用户
    ElasticSearch7.10配置Search-Guard之配置用户配置sg_internal_user.yml密码是:elasticjode:hash:$2y$12$nUzkcjdnufzvI1HlmN7xSuND3skGhmwV5le5IINejz.asMFpLYNRy......
  • ElasticSearch必知必会-进阶篇
    京东物流:康睿姚再毅李振刘斌王北永说明:以下全部均基于elasticsearch 8.1版本一.跨集群检索-ccr官网文档地址:​​​https://www.elastic.co/guide/en/elasticsearch/......
  • ElasticSearch必知必会-进阶篇
    京东物流:康睿姚再毅李振刘斌王北永说明:以下全部均基于elasticsearch8.1版本一.跨集群检索-ccr官网文档地址:https://www.elastic.co/guide/en/elasticsearch/re......