Skip to content

Commit

Permalink
DiscoveryV2 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
apaparazzi0329 committed Oct 30, 2020
1 parent 776b35b commit af4e720
Show file tree
Hide file tree
Showing 9 changed files with 713 additions and 283 deletions.
610 changes: 419 additions & 191 deletions Sources/DiscoveryV2/Discovery.swift

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions Sources/DiscoveryV2/Models/AnalyzedDocument.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* (C) Copyright IBM Corp. 2020.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import Foundation

/**
An object containing the converted document and any identified enrichments.
*/
public struct AnalyzedDocument: Codable, Equatable {

/**
Array of document results that match the query.
*/
public var notices: [Notice]?

/**
Result of the document analysis.
*/
public var result: AnalyzedResult?

// Map each property name to the key that shall be used for encoding/decoding.
private enum CodingKeys: String, CodingKey {
case notices = "notices"
case result = "result"
}

}
53 changes: 53 additions & 0 deletions Sources/DiscoveryV2/Models/AnalyzedResult.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* (C) Copyright IBM Corp. 2020.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import Foundation
import IBMSwiftSDKCore

/**
Result of the document analysis.
*/
public struct AnalyzedResult: Codable, Equatable {

/**
Metadata of the document.
*/
public var metadata: [String: JSON]?

/// Additional properties associated with this model.
public var additionalProperties: [String: JSON]

// Map each property name to the key that shall be used for encoding/decoding.
private enum CodingKeys: String, CodingKey {
case metadata = "metadata"
static let allValues = [metadata]
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
metadata = try container.decodeIfPresent([String: JSON].self, forKey: .metadata)
let dynamicContainer = try decoder.container(keyedBy: DynamicKeys.self)
additionalProperties = try dynamicContainer.decode([String: JSON].self, excluding: CodingKeys.allValues)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(metadata, forKey: .metadata)
var dynamicContainer = encoder.container(keyedBy: DynamicKeys.self)
try dynamicContainer.encodeIfPresent(additionalProperties)
}

}
6 changes: 0 additions & 6 deletions Sources/DiscoveryV2/Models/CollectionDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,21 @@ public struct CollectionDetails: Codable, Equatable {
Initialize a `CollectionDetails` with member variables.

- parameter name: The name of the collection.
- parameter collectionID: The unique identifier of the collection.
- parameter description: A description of the collection.
- parameter created: The date that the collection was created.
- parameter language: The language of the collection.
- parameter enrichments: An array of enrichments that are applied to this collection.

- returns: An initialized `CollectionDetails`.
*/
public init(
name: String,
collectionID: String? = nil,
description: String? = nil,
created: Date? = nil,
language: String? = nil,
enrichments: [CollectionEnrichment]? = nil
)
{
self.name = name
self.collectionID = collectionID
self.description = description
self.created = created
self.language = language
self.enrichments = enrichments
}
Expand Down
186 changes: 120 additions & 66 deletions Sources/DiscoveryV2/Models/QueryAggregation.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2019.
* (C) Copyright IBM Corp. 2020.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,91 +16,145 @@

import Foundation

/** An aggregation produced by the Discovery service to analyze the input provided. */
/**
An abstract aggregation type produced by Discovery to analyze the input provided.
*/
public enum QueryAggregation: Codable, Equatable {

// reference: https://cloud.ibm.com/docs/services/discovery/query-reference.html#aggregations

case queryAggregation(GenericQueryAggregation)
case term(QueryTermAggregation)
case filter(QueryFilterAggregation)
case nested(QueryNestedAggregation)
case histogram(QueryHistogramAggregation)
case timeslice(QueryTimesliceAggregation)
case topHits(QueryTopHitsAggregation)
case uniqueCount(QueryCalculationAggregation)
case max(QueryCalculationAggregation)
case nested(QueryNestedAggregation)
case filter(QueryFilterAggregation)
case min(QueryCalculationAggregation)
case average(QueryCalculationAggregation)
case max(QueryCalculationAggregation)
case sum(QueryCalculationAggregation)
case generic(GenericQueryAggregation)

private enum CodingKeys: String, CodingKey {
case type = "type"
}
case average(QueryCalculationAggregation)
case uniqueCount(QueryCalculationAggregation)
case topHits(QueryTopHitsAggregation)
case groupBy(QueryGroupByAggregation)

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
guard let type = try container.decodeIfPresent(String.self, forKey: .type) else {
// the specification does not identify `type` as a required field,
// so we need a generic catch-all in case it is not present
self = .generic(try GenericQueryAggregation(from: decoder))
return
}
switch type {
case "term": self = .term(try QueryTermAggregation(from: decoder))
case "filter": self = .filter(try QueryFilterAggregation(from: decoder))
case "nested": self = .nested(try QueryNestedAggregation(from: decoder))
case "histogram": self = .histogram(try QueryHistogramAggregation(from: decoder))
case "timeslice": self = .timeslice(try QueryTimesliceAggregation(from: decoder))
case "top_hits": self = .topHits(try QueryTopHitsAggregation(from: decoder))
case "unique_count": self = .uniqueCount(try QueryCalculationAggregation(from: decoder))
case "max": self = .max(try QueryCalculationAggregation(from: decoder))
case "min": self = .min(try QueryCalculationAggregation(from: decoder))
case "average": self = .average(try QueryCalculationAggregation(from: decoder))
case "sum": self = .sum(try QueryCalculationAggregation(from: decoder))
default: self = .generic(try GenericQueryAggregation(from: decoder))
let container = try decoder.singleValueContainer()
if let genericInstance = try? container.decode(GenericQueryAggregation.self) {
switch genericInstance.type {
case "term":
if let val = try? container.decode(QueryTermAggregation.self) {
self = .term(val)
return
}
case "histogram":
if let val = try? container.decode(QueryHistogramAggregation.self) {
self = .histogram(val)
return
}
case "timeslice":
if let val = try? container.decode(QueryTimesliceAggregation.self) {
self = .timeslice(val)
return
}
case "nested":
if let val = try? container.decode(QueryNestedAggregation.self) {
self = .nested(val)
return
}
case "filter":
if let val = try? container.decode(QueryFilterAggregation.self) {
self = .filter(val)
return
}
case "min":
if let val = try? container.decode(QueryCalculationAggregation.self) {
self = .min(val)
return
}
case "max":
if let val = try? container.decode(QueryCalculationAggregation.self) {
self = .max(val)
return
}
case "sum":
if let val = try? container.decode(QueryCalculationAggregation.self) {
self = .sum(val)
return
}
case "average":
if let val = try? container.decode(QueryCalculationAggregation.self) {
self = .average(val)
return
}
case "unique_count":
if let val = try? container.decode(QueryCalculationAggregation.self) {
self = .uniqueCount(val)
return
}
case "top_hits":
if let val = try? container.decode(QueryTopHitsAggregation.self) {
self = .topHits(val)
return
}
case "group_by":
if let val = try? container.decode(QueryGroupByAggregation.self) {
self = .groupBy(val)
return
}
default:
if let val = try? container.decode(GenericQueryAggregation.self) {
self = .queryAggregation(val)
return
}
}
}

throw DecodingError.typeMismatch(QueryAggregation.self,
DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Decoding failed for all associated types"))
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
var container = encoder.singleValueContainer()
switch self {
case .queryAggregation(let queryAggregation):
try container.encode(queryAggregation)
case .term(let term):
try container.encode("term", forKey: .type)
try term.encode(to: encoder)
case .filter(let filter):
try container.encode("filter", forKey: .type)
try filter.encode(to: encoder)
case .nested(let nested):
try container.encode("nested", forKey: .type)
try nested.encode(to: encoder)
try container.encode(term)
case .histogram(let histogram):
try container.encode("histogram", forKey: .type)
try histogram.encode(to: encoder)
try container.encode(histogram)
case .timeslice(let timeslice):
try container.encode("timeslice", forKey: .type)
try timeslice.encode(to: encoder)
case .topHits(let topHits):
try container.encode("top_hits", forKey: .type)
try topHits.encode(to: encoder)
case .uniqueCount(let uniqueCount):
try container.encode("unique_count", forKey: .type)
try uniqueCount.encode(to: encoder)
case .max(let max):
try container.encode("max", forKey: .type)
try max.encode(to: encoder)
try container.encode(timeslice)
case .nested(let nested):
try container.encode(nested)
case .filter(let filter):
try container.encode(filter)
case .min(let min):
try container.encode("min", forKey: .type)
try min.encode(to: encoder)
case .average(let average):
try container.encode("average", forKey: .type)
try average.encode(to: encoder)
try container.encode(min)
case .max(let max):
try container.encode(max)
case .sum(let sum):
try container.encode("sum", forKey: .type)
try sum.encode(to: encoder)
case .generic(let generic):
try generic.encode(to: encoder)
try container.encode(sum)
case .average(let average):
try container.encode(average)
case .uniqueCount(let unique_count):
try container.encode(unique_count)
case .topHits(let top_hits):
try container.encode(top_hits)
case .groupBy(let group_by):
try container.encode(group_by)
}
}
}

public struct GenericQueryAggregation: Codable, Equatable {

/**
The type of aggregation command used. Options include: term, histogram, timeslice, nested, filter, min, max, sum,
average, unique_count, and top_hits.
*/
public var type: String

// Map each property name to the key that shall be used for encoding/decoding.
private enum CodingKeys: String, CodingKey {
case type = "type"
}

}
8 changes: 7 additions & 1 deletion Sources/DiscoveryV2/Models/QueryResponse.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2019, 2020.
* (C) Copyright IBM Corp. 2020.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,11 @@ public struct QueryResponse: Codable, Equatable {
*/
public var tableResults: [QueryTableResult]?

/**
Passages returned by Discovery.
*/
public var passages: [QueryResponsePassage]?

// Map each property name to the key that shall be used for encoding/decoding.
private enum CodingKeys: String, CodingKey {
case matchingResults = "matching_results"
Expand All @@ -65,6 +70,7 @@ public struct QueryResponse: Codable, Equatable {
case suggestedQuery = "suggested_query"
case suggestedRefinements = "suggested_refinements"
case tableResults = "table_results"
case passages = "passages"
}

}
Loading

0 comments on commit af4e720

Please sign in to comment.