gpt4 book ai didi

elasticsearch - Elastic Search 传递的对象个数必须是偶数

转载 作者:行者123 更新时间:2023-11-29 02:44:46 24 4
gpt4 key购买 nike

我正在学习 Elasticsearch ,我正在关注 the next tutorial , 但我得到下一个错误

Exception in thread "main" java.lang.IllegalArgumentException: The number of object passed must be even but was [1]
at org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:451)
at elastic.elasti.App.lambda$0(App.java:55)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at elastic.elasti.App.indexExampleData(App.java:53)
at elastic.elasti.App.main(App.java:45)

你能帮我解决一下吗?

public class App 
{
public static void main( String[] args ) throws TwitterException, UnknownHostException
{
System.out.println( "Hello World!" );
List tweetJsonList = searchForTweets();

Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
String index = "tweets_juan";
client.admin().indices()
.create(new CreateIndexRequest(index))
.actionGet();
indexExampleData(client, tweetJsonList, index);
searchExample(client);
}
public static void indexExampleData(Client client, List tweetJsonList, String index) {


BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

tweetJsonList.forEach((jsonTweet) -> {
bulkRequestBuilder.add(new IndexRequest(index, "tweets_juan")
.source(jsonTweet));
});

BulkResponse bulkItemResponses = bulkRequestBuilder.get();
}




public static void searchExample(Client client) {
BoolQueryBuilder queryBuilder = QueryBuilders
.boolQuery()
.must(termsQuery("text", "españa"));

SearchResponse searchResponse = client.prepareSearch("tweets_juan")
.setQuery(queryBuilder)
.setSize(25)
.execute()
.actionGet();
}

public static List searchForTweets() throws TwitterException {
Twitter twitter = new TwitterFactory().getInstance();
Query query = new Query("mundial baloncesto");
List tweetList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
QueryResult queryResult = twitter.search(query);
tweetList.addAll(queryResult.getTweets());
if (!queryResult.hasNext()) {
break;
}
query = queryResult.nextQuery();
}
Gson gson = new Gson();

return (List) tweetList.stream().map(gson::toJson).collect(Collectors.toList());
}
}

最佳答案

我知道已经晚了,但对此的简单回答是添加 XContentType.JSON 以及 ElasticSearch 库包 org.elasticsearch.common 中可用的源代码.xcontent

public static void indexExampleData(Client client, List tweetJsonList, String index) {


BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

tweetJsonList.forEach((jsonTweet) -> {
bulkRequestBuilder.add(new IndexRequest(index, "tweets_juan")
.source(jsonTweet,XContentType.JSON));
});

BulkResponse bulkItemResponses = bulkRequestBuilder.get();
}

关于elasticsearch - Elastic Search 传递的对象个数必须是偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39187097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com