2020年elasticarch7.9搜索模板restHighLevelClient操。。。⽂章⽬录
前⾔
说到elasticarch,⽬前已经更新到最新7.9了,这样⽤于做全⽂搜索等。
如果想学得更加精进⼀点,建议还是看官⽹,但是只是接触 搜索模板,可以看我以下的内容
本⽂主要讲的是 Java High Level REST Client 使⽤该java客户端对 搜索模板进⾏操作的
⽣产出测试数据
以下测试均是⽤java代码接⼝请求, 可以参考后⾯ java完整代码
其中 cantemplate-index 为索引名称POST localhost:8888/template/addData
{
"topic":"cantemplate-index", "templateJson":{
"name":"jack",
"age":18,
"x":"男",
"address":"⼴州"
}
}
{
"topic":"cantemplate-index", "templateJson":{
"name":"jafast",
"age":21,
"x":"男",
"address":"珠海"
}
}
{
"topic":"cantemplate-index", "templateJson":{
"name":"lili",
"age":19,
"x":"⼥",
"address":"⼴州"
}
}
{
"topic":"cantemplate-index", "templateJson":{
"name":"mong",
"age":20,
"x":"⼥",
"address":"南宁"
}
}
{
"topic":"cantemplate-index", "templateJson":{
"name":"xindy",
"age":21,
"x":"男",
"address":"珠海"
}
}
创建模板,注意{{}}语法
模板写法---下⾯只是参考,跟本⽂例⼦⽆关
{"templateJson":{
"script":{
"lang":"mustache",
"source":"{\"query\": {\"bool\": {\"filter\": [{{#data-name}}{\"match\": {\"{{data-name}}\":\"{{dataname}}\"}}{{#data-cccc}},{{/data-cccc}}{{/data-name}}{{#dat a-cccc}}{\"match\": {\"{{data-cccc}}\":\"{{datacccc}}\"}}{{/data-cccc}}]}}}"
}
}}
由来
{
"query":{
"bool":{
"must":[
{
"match":{
"address":"珠海"
}
},
{
"range":{
"age":{
"gt":"18",
"lt":"22"
}
}
}
]
}
}
}
创建模板,使⽤{{}}来确定变量
其中 cantemplate-index1为模板名称
POST localhost:8888/template/createEsTemplate/cantemplate1
{"templateJson":{
"script":{
"lang":"mustache",
"source":"{\"query\":{\"bool\":{\"must\":[{\"match\":{\"address\":\"{{address}}\"}},{\"range\":{\"age\":{\"gt\":\"{{gt}}\",\"lt\":\"{{lt}}\"}}}]}}}"
}
}}
或者
添加判断条件,避免map不传⼊报错{{#}}{{/}}组合来进⾏判断
{"templateJson":{
"script":{
"lang":"mustache",
"source":"{\"query\":{\"bool\":{\"must\":[{{#address}}{\"match\":{\"address\":\"{{address}}\"}}{{/address}}{{#gt}}{{#lt}}{{#address}},{{/address}}{\"range\":{\"a ge\":{\"gt\":\"{{gt}}\",\"lt\":\"{{lt}}\"}}}{{/lt}}{{/gt}}]}}}"
}
}}
使⽤创建的模板进⾏查询
其中 cantemplate1 为模板名称,cantemplate-index为索引名称
POST localhost:8888/template/uEsTemplate/cantemplate-index/cantemplate1
{
"address":"⼴州",
"lt":"22",
"gt":"18"
}
结果
[
{
"address":"⼴州",
"x":"⼥",
"name":"lili",
"id":"72AACAB68DCB42369966861EFAFD4A78",
"age":19
}
]
查看使⽤模板的ES Query SQL, 需要带上参数
其中 cantemplate1 为索引名称
绿茶冲泡温度POST localhost:8888/template/getestemplate/cantemplate1
腐乳鸡翅{
"address":"⼴州",
"lt":"20",
"gt":"17"
事业单位工作人员年度考核个人总结
}
结果
{
"query":{
"bool":{
"must":[
{
"match":{
"address":"⼴州"
}
},
{
"range":{
"age":{
红酒煮梨
"lt":"22",
"gt":"18"
}
}
}
]
}
}
}
精华 java完整代码
依赖(注意需要elasticarch,elasticarch-rest-client依赖版本)
<properties>
<java.version>1.8</java.version>
<elasticarch>7.5.1</elasticarch>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticarch.client</groupId>
<artifactId>elasticarch-rest-high-level-client</artifactId>
<version>${elasticarch}</version>
</dependency>
<dependency>
<groupId>org.elasticarch</groupId>
<artifactId>elasticarch</artifactId>
<version>${elasticarch}</version>
</dependency>
<dependency>
<groupId>org.elasticarch.client</groupId>
<artifactId>elasticarch-rest-client</artifactId>
<version>${elasticarch}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticarch</artifactId> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
跑步怎么跑得快<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>死神的英文
<scope>test</scope>
<exclusions>
<exclusion>
imei查询>广什么无垠<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
application