2017-08-27 115 views
2

我想開發一個Java程序來查詢沃森發現新聞。我的目標是過濾指定日期範圍內的熱門新聞。以下是我在程序中使用的查詢。是否有任何API,以便我可以在頂級故事標題和相應的網站鏈接上獲得過濾值?WATSON發現新聞Java API獲取最新的故事

{ 
    "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" 
} 

在此先感謝。

回答

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> 
... 

不幸的是,發現新聞的文件沒有明確說明如何可以訪問默認的系統消息的數據倉庫,這裏的關鍵是要使用這些值

... 
      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/" 
    } 
    ] 
}