elasticsearch - Elasticsearch 和Twitter数据示例

标签 elasticsearch twitter

我正在学习 flex 搜索,并且正在关注the next tutorial。在该教程中,将Twiter的推文用作示例数据。方法tweetJsonList返回示例数据。我试图将其保存在索引“tweets_juan”中,然后键入“tweet”。该应用程序运行没有问题,但是当我使用(http://localhost:9200/tweets_juan/tweet/_search?q= :)搜索所有文档时,我什么都没找到。您能帮我验证一下这里发生了什么吗?

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

最佳答案

您需要先提供更多信息,然后任何人才能回答您的问题。

由于未使用任何显式映射,因此默认情况下必须对字段进行分析。因此,您的文本字段将被标记为多个术语。

  • 使用“全部匹配”查询来查看已对哪些数据建立索引。
  • 术语查询用于完全匹配(包括精确的大小写),并且您正在尝试对无法使用的已分析字段“文本”运行术语查询。
  • 尝试在文本字段上使用匹配或匹配短语查询,看看是否返回任何结果。
  • 关于elasticsearch - Elasticsearch 和Twitter数据示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39188679/

    相关文章:

    solr - 如何上载/索引丰富/结构化文本文档以使用ElasticSearch进行搜索?

    json - 如何在 bash 中将 JSON 子元素字符串转换为真正的 JSON 元素

    node.js - 使用 Node js 从 elasticsearch 中分段获取数据

    Twitter 时间日期戳 - 这是哪个时区?

    ruby - Twitter created_at 时间到 ruby​​ 中的 unix 时间戳

    sql-server - 如何在我的搜索中集成 Elasticsearch

    php - 如何在ElasticSearch中的某些索引中搜索

    android - 如何处理推特和 Facebook 请求码

    php - Cron 作业 + 推特

    java - Twitter4j.获取来自国家/地区的用户