用于获取热门故事的Watson Discovery News Java API

时间:2017-08-27 00:48:41

标签: java ibm-watson service-discovery watson-discovery

我正在尝试开发一个Java程序来查询Watson Discovery新闻。我的目标是过滤指定日期范围的热门故事。下面是我在程序中使用的查询。是否有任何API,以便我可以在Top story标题和相应的站点链接上获得过滤值?

{
  "query": "\"IBM\",language:(english|en)",
  "filter": "crawl_date>2017-06-26T12:00:00-0400,crawl_date<2017-08-26T12:00:00-0400",
  "count": 5,
  "return": "title,url,host,crawl_date"
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

是的,它可能,您的代码将如下所示

import com.ibm.watson.developer_cloud.discovery.v1.Discovery;
import com.ibm.watson.developer_cloud.discovery.v1.model.QueryOptions;
import com.ibm.watson.developer_cloud.discovery.v1.model.QueryResponse;

public class DiscoveryNewsDemo {

    public static void main(String[] args) {
        Discovery discovery = new Discovery("2017-09-01");
        discovery.setEndPoint("https://gateway.watsonplatform.net/discovery/api/");
        discovery.setUsernameAndPassword("<username>", "<password>"); //replace with the appropriate values here

        String environmentId = "system";
        String collectionId = "news";
        QueryOptions queryOptions = new QueryOptions.Builder(environmentId, collectionId)
                .query("IBM,language:(english|en)")
                .filter("crawl_date>2017-06-26T12:00:00-0400,crawl_date<2017-08-26T12:00:00-0400")
                .count(5)
                .addReturnField("title")
                .addReturnField("url")
                .addReturnField("host")
                .addReturnField("crawl_date")
                .build();
        QueryResponse queryResponse = discovery.query(queryOptions).execute();

        System.out.println(queryResponse);
    }

}

但是,请注意发现新闻似乎只保留最近90天的数据,因此很可能您必须更换

.filter("crawl_date>2017-06-26T12:00:00-0400,crawl_date<2017-08-26T12:00:00-0400")

最新的东西(我在2017年11月27日写这个答案),例如

.filter("crawl_date>2017-10-26T12:00:00-0400,crawl_date<2017-11-26T12:00:00-0400")

如果您正在使用maven,则需要添加此依赖项

...
  <dependencies>
    <dependency>
        <groupId>com.ibm.watson.developer_cloud</groupId>
        <artifactId>discovery</artifactId>
        <version>4.0.0</version>
    </dependency>
  </dependencies>
...

不幸的是,Discovery News文档没有明确说明你如何访问默认的系统新闻数据存储库,这里的技巧就是使用这些值

...
            String environmentId = "system";
            String collectionId = "news";
            QueryOptions queryOptions = new QueryOptions.Builder(environmentId, collectionId)
...

它会返回这样的结果

Nov 27, 2017 8:29:04 AM okhttp3.internal.platform.Platform log
INFO: --> GET https://gateway.watsonplatform.net/discovery/api/v1/environments/system/collections/news/query?version=2017-09-01&filter=crawl_date%3E2017-10-26T12:00:00-0400,crawl_date%3C2017-11-26T12:00:00-0400&query=IBM,language:(english%7Cen)&count=5&return=title,url,host,crawl_date http/1.1
Nov 27, 2017 8:29:06 AM okhttp3.internal.platform.Platform log
INFO: <-- 200 OK https://gateway.watsonplatform.net/discovery/api/v1/environments/system/collections/news/query?version=2017-09-01&filter=crawl_date%3E2017-10-26T12:00:00-0400,crawl_date%3C2017-11-26T12:00:00-0400&query=IBM,language:(english%7Cen)&count=5&return=title,url,host,crawl_date (1912ms, unknown-length body)
{
  "matching_results": 19922,
  "results": [
    {
      "score": 3.2059636,
      "host": "mcts-mcitp.com",
      "crawl_date": "2017-11-18T06:41:05Z",
      "id": "txAkTtulpLhTd6FqrCCqWhiXfaQFLOz8SQBH7DiC2FqGwOIDPl4udzDBV0_6p0xK",
      "title": "C9020-667 IBM New Workloads Sales V1",
      "url": "http://www.mcts-mcitp.com/2017/11/17/c9020-667-ibm-new-workloads-sales-v1/"
    },
    {
      "score": 3.1392605,
      "host": "developer.com",
      "crawl_date": "2017-11-03T00:38:21Z",
      "id": "h1kKQlSfPKM3guslW9wfVJyyvWoTDv8UHdOksDaqCEC3CV3Ya8sWl6JXDgn02yMN",
      "title": "IBM Renames Its Cloud Computing Service",
      "url": "https://www.developer.com/daily_news/ibm-renames-its-cloud-computing-service.html"
    },
    {
      "score": 3.0926523,
      "host": "prominic.net",
      "crawl_date": "2017-10-30T22:08:03Z",
      "id": "S94YGfvGGL3K84j2i75GmvcSfxJRu9b7CS73vSncmYr3gHBObjbLF7rziVtdOkJn",
      "title": "Prominic.NET Offers Free Hosting to IBM Champions - Prominic.NET",
      "url": "https://prominic.net/2017/05/08/prominic-net-offers-free-hosting-ibm-champions/"
    },
    {
      "score": 3.0866172,
      "host": "mcts-mcitp.com",
      "crawl_date": "2017-11-16T00:32:11Z",
      "id": "KG6QUpkwnv02cD0oi2EN_dSCpcpbfi3JtuIjULVER1cWtxVZ77bV8vWkaHY5atsW",
      "title": "C9020-662 IBM Virtualized Storage V1",
      "url": "http://www.mcts-mcitp.com/2017/11/15/c9020-662-ibm-virtualized-storage-v1/"
    },
    {
      "score": 3.0718818,
      "host": "techrepublic.com",
      "crawl_date": "2017-11-10T15:42:00Z",
      "id": "qgFND-rmtO0F5xkZLqbam-Di2vdjQ2iIkeTR8DqR3vNgturI2AuPJn6sDI_mz_Ct",
      "title": "Conjure VR objects with your voice, using new IBM Watson service - Video | ZDNet",
      "url": "http://www.techrepublic.com/videos/conjure-vr-objects-with-your-voice-using-new-ibm-watson-service/"
    }
  ]
}