1
votes

I am learning about elastic search and I am following the next tutorial. In that tutorial it is used tweets of Twiter as example data. Method tweetJsonList return a example data. I am trying to save this in the index "tweets_juan" and type "tweet". The application run without problems, but when I search all documents using (http://localhost:9200/tweets_juan/tweet/_search?q=:) I do not found anything. Could you help me please to verify whats happens here?

public class App 
{
@SuppressWarnings("unchecked")
public static void main( String[] args ) throws TwitterException, UnknownHostException
{
    System.out.println( "Hello World!" );
    List<String> 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();
    save(client, tweetJsonList, index);
    searchExample(client);
}

public static void save(Client client, List<String> tweetJsonList, String index) {

    BulkRequestBuilder bulkRequestBuilder = client.prepareBulk().setRefresh(true);
    for (String data :  tweetJsonList) {
        String indexName = index;
        String typeName = "tweet";

        String json = new Gson().toJson(data);
        System.out.println("Juan Debug:" + data);
        bulkRequestBuilder.add(client.prepareIndex(indexName, typeName).setSource(json));
    }
    bulkRequestBuilder.execute().actionGet();
}


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

    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());
}
}
1
Try sarching after setting types in search: client.prepareSearch("tweets_juan").setTypes("tweet") .setQuery(queryBuilder) .setSize(25) .execute() .actionGet();ryanlutgen
@ryanlutgen just I trying that, but I do not get anythingJuan

1 Answers

0
votes

You need to put more information before anyone can answer your question.

Since you are not using any explicit mapping your fields must be getting analyzed by default. So your text field will get tokenized into multiple terms.

  1. Use "match all" query to see what data has been indexed.
  2. Term query is used for exact match ( including exact case) and you are trying to run term query on an analyzed field "text" which will not work.
  3. Try using match or match phrase query on the text field and see if you get back any result.