弹性搜索通过的对象数量必须均匀 [英] Elastic Search number of object passed must be even

查看:1502
本文介绍了弹性搜索通过的对象数量必须均匀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习弹性搜索,我正在关注下一个教程 ,但是我得到下一个错误

 线程main中的异常java.lang.IllegalArgumentException:传递的对象数必须甚至是[1] 
在org.elasticsearch.action.index.IndexRequest.source(IndexRequest.java:451)
在elastic.elasti.App.lambda $ 0(App.java:55)
在java.util.ArrayList.forEach(ArrayList.java:1249)
在elastic.elasti.App.indexExampleData(App.java:53)
在elastic.elasti.App.main( App.java:45)

可以帮我修复吗?

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

客户端客户端= TransportClient.builder()。build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(localhost),9300));
String index =tweets_juan;
client.admin()。indexes()
.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();
查询查询= new Query(mundial baloncesto);
列出tweetList = new ArrayList<>(); (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());
}
}


解决方案


  1. Json对象不能用作索引的来源

  2. 使用像杰克逊这样的东西,或者将地图设为地图

杰克逊:



stringifiedJson = objectMapper.writeValueAsString(jsonObject)


I am learning about elastic search and I am following the next tutorial, but I get the next error

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)

Could you help me to fix it please?

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());
    }
}

解决方案

Summary:

  1. Json object cannot be used as a source for indexing
  2. Either Stringify your json by using something like Jackson or set the source as Map

Jackson:

String stringifiedJson = objectMapper.writeValueAsString(jsonObject)

这篇关于弹性搜索通过的对象数量必须均匀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆