Elasticsearch索引批量数据-Java API

时间:2018-08-03 06:23:58

标签: elasticsearch

根据弹性搜索文档,当我们想一次索引批量数据时,可以使用

BulkRequestBuilder bulkRequest = client.prepareBulk();

// either use client#prepare, or use Requests# to directly build index/delete requests
bulkRequest.add(client.prepareIndex("twitter", "tweet", "1")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                    .endObject()
                  )
        );

bulkRequest.add(client.prepareIndex("twitter", "tweet", "2")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "another post")
                    .endObject()
                  )
        );

如果我们希望将10种不同的数据放入索引,但是我有250余种数据放入索引,这是可以的,我的数据当前为json格式,如下所示

[
  {
    "departure": {
      "city": "\u041c\u0438\u043d\u0441\u043a",
      "date": "Aug 27, 2018 12:09:00 AM"
    },
    "extras": [],
    "hotel": {
      "beach": {
        "distance": 0,
        "type": "\u041f\u0435\u0441\u0447\u0430\u043d\u044b\u0439"
      },
      "country": "\u0413\u0440\u0435\u0446\u0438\u044f",
      "distanceToAirport": 0,
      "facilities": [
        "Standard"
      ],
      "food": "\u0412\u0441\ 4e\u0447\u0435\u043d\u043e",
      "photoUrl": "https://s1.tez-tour.com/hotel/7019851/low_SWIMMING_POOL3_4931_small.jpg",
      "regionName": "\u041a\u0 18\u0440\u0430\u043a\u043b\u0438\u043e\u043d",
      "stars": 4,
      "title": "LAVRIS HOTELS & SPA"
    },
    "nights": 9,
    "people": 1,
    "price": 1967,
    "typeOfRest": "\u0421\u0435\u043c\u0435\u0439\u043d\u044b\u0439 \u0420\u043e\u043c\u0430\u043d\u0442\u0438\u043a \u0410\u043a\u0442\u0438\u0432\u043d\u044b\u0439"
  },
  {
    "departure": {
      "city": "\u041c\u0438\u043d\u0441\u043a",
      "date": "Aug 26, 2018 12:08:00 AM"
    },
    "extras": [],
    "hotel": {
      "beach": {
        "distance": 0,
        "type": "\u041f\u0435\u0441\u0447\u0430\u043d\u044b\u0439"
      },
      "country": "\u0413\u0440\u0435\u0446\u0438\u044f",
      "distanceToAirport": 0,
      "facilities": [
        "Standard"
      ],
      "food": "\u0417\u0430\u0432\u0442\"\u0438\u043d",
      "photoUrl": "https://s.tez-tour.com/hotel/7024462/zpanoramic3_5452_small.jpg",
      "regionName": "\u041a\u0440\u0438\u0442\"
      "stars": 4,
      "title": "GLAROS BEACH HOTEL"
    },
    "nights": 7,
    "people": 1,
    "price": 2217,
    "typeOfRest": "\u0421\u0435\u043c 
  },

我有200多个这样的数据,有没有比一次全部写入字段更好的方法一次索引所有索引了。可以从类路径中调用,因为我所有的数据都在一个名为tour.json

的文件中

0 个答案:

没有答案