为什么这个Spring Boot应用程序仅加载嵌入式Elasticsearch实例?

时间:2018-10-01 13:00:30

标签: spring spring-boot elasticsearch

我正在玩spring-boot-sample-data-elastcisearch项目。

该应用程序有效,但仅适用于嵌入式Elasticsearch。

我希望它与我的本地elasticsearch实例一起运行。 为什么Embed ElasticSearch在此应用程序中运行?

import org.elasticsearch.client.Client;
import org.elasticsearch.node.NodeBuilder;

import java.io.IOException;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;


public class ElasticHelper {

    /**
     * Spin up an elasticsearch node in the background
     */
    public static Client startCluster() {
        NodeBuilder nb = nodeBuilder()
                .local(true)
                .clusterName("autocomplete");

        nb.settings()
                .put("index.number_of_shards", "1")
                .put("index.number_of_replicas", "0")
                .put("path.home", "target/elasticsearch")
                .put("path.data", "target/elasticsearch/data")
                .put("path.logs", "target/elasticsearch/logs")
                .put("path.work", "target/elasticsearch/work");

        return nb.node().client();
    }

    /**
     * Load example data to be used by the demo-app
     */
    public static void loadExampleData(Client client) throws IOException {

        // Delete index if exists
        if (client.admin().indices().prepareExists("provinces").get().isExists()) {
            client.admin().indices().prepareDelete("provinces").get();
        }

        client.admin().indices().prepareCreate("provinces")
                .addMapping("province",
                        jsonBuilder()
                            .startObject()
                            .field("province")
                                .startObject()
                                .field("properties")
                                    .startObject()
                                    .field("suggest")
                                        .startObject()
                                        .field("type", "completion")
                                        .endObject()
                                    .field("name")
                                        .startObject()
                                        .field("type", "string")
                                        .endObject()
                                    .field("capital")
                                        .startObject()
                                        .field("type", "string")
                                        .endObject()
                                    .endObject()
                                .endObject()
                            .endObject())
                .get();

        addSuggestion(client, "Groningen", "Groningen");
        addSuggestion(client, "Friesland", "Leeuwarden");
        addSuggestion(client, "Drente", "Assen");
        addSuggestion(client, "Overijssel", "Zwolle");
        addSuggestion(client, "Gelderland", "Arnhem");
        addSuggestion(client, "Utrecht", "Utrecht");
        addSuggestion(client, "Flevoland", "Lelystad");
        addSuggestion(client, "NoordHolland", "Haarlem");
        addSuggestion(client, "ZuidHolland", "Den Haag");
        addSuggestion(client, "Zeeland", "Middelburg");
        addSuggestion(client, "Brabant", "Den Bosch");
        addSuggestion(client, "Limburg", "Maastricht");
    }

    /**
     * Index(load) data for one province into the elastic cluster
     */
    private static void addSuggestion(Client client, String name, String capital) throws IOException {
        client.prepareIndex("provinces", "province")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("suggest", name)
                        .field("name", name)
                        .field("capital", capital)
                        .endObject())
                .get();
    }

}

http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0

<groupId>nl.dehling</groupId>
<artifactId>searchAndAutocomplete</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>searchAndAutocomplete</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </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>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>2.4.1</version>
    </dependency>

    <dependency>
        <groupId>net.java.dev.jna</groupId>
        <artifactId>jna</artifactId>
        <version>4.2.2</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.0</version>
            <configuration>
                <installDirectory>target</installDirectory>
            </configuration>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v4.4.5</nodeVersion>
                        <npmVersion>3.9.2</npmVersion>
                    </configuration>
                </execution>
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>webpack build</id>
                    <goals>
                        <goal>webpack</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

0 个答案:

没有答案