2016-08-28 8 views
7

olmalıdır geçirilen nesne sayısı, ama gelecek hatayıElastik ara ben elastik arama hakkında öğreniyorum ve ben <a href="https://www.adictosaltrabajo.com/tutoriales/primeros-pasos-elasticsearch/" rel="noreferrer">the next tutorial</a> takip ediyorum hatta

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) 

olsun bana lütfen düzeltmek için yardım eder misiniz?

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

cevap

6

Özet:

  1. Json nesnesi Jackson gibi bir şey kullanarak endeksleme
  2. Ya stringify sizin json için bir kaynak olarak kullanılabilir veya Harita
  3. olarak kaynağını ayarlanamaz

Jackson :

Dize stringifiedJson = objectMapper.writeValueAsString (jsonObject)

1

JSON içeriğini yönetmek ve bu hatayla karşılaşmaya org.json kütüphane kullanırsanız, bu şekilde (skgemini his answer önerildiği gibi Haritası gibi kaynağını ayarlama) çözebilir:

JSONObject dataAsJson = new JSONObject(dataAsJsonFormattedString); 
HashMap<String, Object> dataAsMap = new HashMap<String, Object>(dataAsJson.toMap()); 
bulkRequestBuilder.add(new IndexRequest(index, "tweets_juan").source(dataAsMap, XContentType.JSON)); 
İlgili konular