繁体   English   中英

如何将文档添加到我的弹性搜索中?

[英]How can i add documents to my elastic search?

嗨,我有 maven 项目。 我正在尝试使用弹性搜索。 所以我有一个 Junit 测试类,我试图在其中启动一个嵌入式弹性搜索实例。 我可以创建一个名为 cars 的索引,然后在 cars 索引中键入 car 。 我想做的是将 100 辆车填入索引。

所以以下是我的代码。

public class ElasticSearchTest {

    private static EmbeddedElastic embeddedElastic;

    @BeforeClass
    public static void init() throws IOException, InterruptedException {

        embeddedElastic = EmbeddedElastic.builder().withElasticVersion("6.1.1")
                .withSetting(PopularProperties.TRANSPORT_TCP_PORT, 9350)
                .withSetting(PopularProperties.CLUSTER_NAME, "my_cluster")
                .withStartTimeout(2, TimeUnit.MINUTES)
                  .withIndex("cars", IndexSettings.builder()
                            .withType("car", getSystemResourceAsStream())
                            .build())
                .build()
                .start();

    }

    private static InputStream getSystemResourceAsStream() throws FileNotFoundException {

        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream("car-mapping.json");
        return is;
    }

    @Test
    public void test() {
        System.out.println("Hello world");
    }

    @AfterClass
    public static void close() {
        embeddedElastic.stop();
    }

}

汽车地图.json

{
  "car": {
    "properties": {
      "manufacturer": {
        "type": "text",
        "index": "false"
      },
      "model": {
        "type": "text",
        "index": "true"
      },
      "description": {
        "type": "text"
      }
    }
  }
}

我如何用我的 junit 测试中的一些数据填充索引

非常感谢任何帮助谢谢

我做了一些可能对你有帮助的代码,我使用了一些文本编辑器,所以可能有一些错误/未使用的导入,但你可以了解如何去做。

package your.package;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.swing.text.MaskFormatter;

import com.google.gson.JsonObject;
import com.opencsv.CSVReader;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;
import org.apache.http.HttpHost;


import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;


@Component
public class App implements CommandLineRunner {

    @Override
    public void run(String... args)
            throws Exception, NullPointerException, IllegalStateException, IllegalArgumentException {

        String host = "localhost";

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(host, 9200, "http")).setMaxRetryTimeoutMillis(90000000));

        BulkRequest bulk = new BulkRequest();
        IndexRequest indexRequest = null;


        JsonObject jsonDoc = null;
        jsonDoc = new JsonObject();


     //JSON parser object to parse read file
     JSONParser jsonParser = new JSONParser();

     try (FileReader reader = new FileReader("car-mapping.json"))
     {
         //Read JSON file
         Object obj = jsonParser.parse(reader);

         JSONArray carMapping = (JSONArray) obj;

         //Iterate over employee array
         carMapping.forEach(emp -> parseCarMappingObject( (JSONObject) emp ) );

     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     } catch (ParseException e) {
         e.printStackTrace();
     }


     // Close your parser /  Elastic Client
        jsonParser.close();
        client.close();

  }

  private static void parseCarMappingObject(JSONObject employee)
    {
        //Get employee object within list
        JSONObject properties = (JSONObject) employee.get("properties");

        //Get employee first name
        String manufacturer = (String) employeeObject.get("manufacturer");  

        // Get your data, depending on how your json are.
        // Create one JSON object with the data you want to Index.

        // For example
        jsonDoc.addProperty("manufacturer", manufacturer);

        // Then, Index your Data.

        // I generally use _doc as doc type, you can change to whatever you want
        indexRequest = new IndexRequest("your_index_name", "_doc").source(jsonDoc.toString(),
        XContentType.JSON);


        // If your data is too big, use bulk to index it faster
        bulk.add(indexRequest);

        // Do your logic here to Index it depending on its size.
        if (index % 10000 == 0 && index > 0) {
          client.bulk(bulk, RequestOptions.DEFAULT);
          bulk.requests().clear();
        } else if (index > 740000) {
          client.index(indexRequest, RequestOptions.DEFAULT);
        }
    }

  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM