SpringBoot integration ElasticsearchBboss

A: About ElasticsearchBboss

About ElasticsearchBboss description, please see the link Portal

Two: Record SpringBoot integration process

1 is introduced dependence

 <!--ElasticsearchBboss启动类引入-->
 <dependency>
            <groupId>com.bbossgroups.plugins</groupId>
            <artifactId>bboss-elasticsearch-spring-boot-starter</artifactId>
            <version>5.7.5</version>
</dependency>

2 Configure the cluster address

If https, it should be noted that a slightly different wording in the document refer to the portal


#springboot maven项目ESBboss配置文件
spring.elasticsearch.bboss.elasticsearch.rest.hostNames=127.0.0.1:9200
#多集群地址由逗号分隔,例,如下:
#spring.elasticsearch.bboss.elasticsearch.rest.hostNames=10.180.211.27:9200,10.180.211.28:9200,10.180.211.29:9200

If the x-pack or searchguard security authentication is enabled, use the following two properties in application.properties configure account and password, as described in the documentation portal

Extended configuration

#http链接池配置
http.timeoutConnection = 50000
http.timeoutSocket = 50000
http.connectionRequestTimeout=50000
http.retryTime = 1
http.maxLineLength = -1
http.maxHeaderCount = 200
http.maxTotal = 400
http.defaultMaxPerRoute = 200

# dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
dslfile.refreshInterval = -1

 

3. Use the DSL query springBoot

3.1 to establish a mapping entity classes 

If the field is not the same type of mapping is null, but does not complain, more flexible

public class Demo {
    private String id;
    private String name;
    private String tweet;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTweet() {
        return tweet;
    }

    public void setTweet(String tweet) {
        this.tweet = tweet;
    }

    @Override
    public String toString() {
        return "Demo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", tweet='" + tweet + '\'' +
                '}';
    }
}

3.2 injection ClientInterface introduced a service provider class xml

@Component
public class TestService {

    /**
     * 创建加载配置文件的客户端工具,用来检索文档,单实例多线程安全
     */
    ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/dsl.xml");

    public ESDatas<Demo> dslOne(Map<String, Object> params) {

        /**
         *执行查询,demo为索引表,_search为检索操作action
         * ESDatas包含当前检索的记录集合,最多10条记录,由dsl中的size属性指定
         * demo为索引表,_search为检索操作action
         * @param searchDatas esmapper/demo.xml中定义的dsl语句
         * @param params 变量参数
         * @param Demo.class  返回的文档封装对象类型
         */
        ESDatas<Demo> esDatas = clientUtil.searchList("us/tweet/_search", "scriptDsl", params, Demo.class);

        return esDatas;
    }

}

xml as follows

dbeb40371816d6a1fa2d09a3cca7ae9972c.jpg

<property name="scriptDsl">
    <![CDATA[
        {
          "query": {
            "match": {
              "name":#[name]
            }
          },
           "size":10
        }
    ]]>
</property>

3.3 Test Parameter Request

@RequestMapping("/test")
@Controller
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping("/dsl")
    @ResponseBody
    public ESDatas<Demo> DslTest() {

        Map<String, Object> params = new HashMap<String, Object>();
        //设置applicationName1和applicationName2两个变量的值
        params.put("name", "John");

        ESDatas<Demo> esDatas = testService.dslOne(params);

        return esDatas;
    }
}

The results are as follows

f25309413663977574352eefd419a08c943.jpg

 

Reproduced in: https: //my.oschina.net/fusublog/blog/3060127

Guess you like

Origin blog.csdn.net/weixin_33779515/article/details/91942779