elasticesearch 5.5.1 search template java api 实例

pom实例:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.smoner.study</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>1.0-SNAPSHOT</version>
<dependencies>
    
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>5.5.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.10.0</version>
        <classifier>sources</classifier>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.6</version>
    </dependency>
    <!--<dependency>-->
        <!--<groupId>org.postgresql</groupId>-->
        <!--<artifactId>postgresql</artifactId>-->
        <!--<version>9.4.1212.jre7-json</version>-->
    <!--</dependency>-->
    <!--<dependency>-->
        <!--<groupId>org.postgresql</groupId>-->
        <!--<artifactId>postgresql</artifactId>-->
        <!--<version>9.4.1212.jre7</version>-->
    <!--</dependency>-->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.49</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-test-framework</artifactId>
        <version>5.5.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.test</groupId>
        <artifactId>framework</artifactId>
        <version>5.5.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

 

java实例:

package com.smoner.study.elasticsearch.biprice;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.SearchTemplateRequestBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by fengjqc on 2018/9/26.
 */
public class BiPriceTemplateSaveService {
    public static void main(String[] args) throws Exception{
        Settings settings = Settings.builder().put("cluster.name","elasticsearch").build();
        TransportClient transportClient = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300));

        transportClient.admin().cluster().preparePutStoredScript()
                .setLang("mustache")
                .setId("ycbiprice-temp-01")
                .setContent(new BytesArray("{\"template\": {\"query\": {\"match\": {\"vsupply_name.keyword\": \"{{query_string}}\"}}}}"), XContentType.JSON)
                .get();

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("query_string","唐山君安仪器仪表有限公司");
        SearchResponse searchResponse = new SearchTemplateRequestBuilder(transportClient)
                .setScript("ycbiprice-temp-01")
                .setScriptType(ScriptType.STORED)
                .setScriptParams(map)
                .setRequest(new SearchRequest("yc-biprice").types("price"))
                .get()
                .getResponse();

        if (searchResponse.getHits().getHits().length>0){
            for(SearchHit searchHit : searchResponse.getHits().getHits()){
                System.out.println(searchHit.getSourceAsString());
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值