Skip to content

Commit

Permalink
fix(search): do not overwrite existing query filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Fluf22 committed Aug 12, 2024
1 parent 4334c90 commit 3ecb3ec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ struct DisjunctiveFacetingHelper {
var queries = [Query]()

var mainQuery = query
mainQuery.filters = buildFilters(excluding: .none)
if let mainQueryFilters = mainQuery.filters, !mainQueryFilters.isEmpty {
mainQuery.filters = mainQueryFilters + " AND "
}
mainQuery.filters = (mainQuery.filters ?? "") + buildFilters(excluding: .none)

queries.append(mainQuery)

Expand All @@ -59,7 +62,10 @@ struct DisjunctiveFacetingHelper {
.forEach { disjunctiveFacet in
var disjunctiveQuery = query
disjunctiveQuery.facets = [disjunctiveFacet]
disjunctiveQuery.filters = buildFilters(excluding: disjunctiveFacet)
if let disjunctiveQueryFilters = disjunctiveQuery.filters, !disjunctiveQueryFilters.isEmpty {
disjunctiveQuery.filters = disjunctiveQueryFilters + " AND "
}
disjunctiveQuery.filters = (disjunctiveQuery.filters ?? "") + buildFilters(excluding: disjunctiveFacet)
disjunctiveQuery.hitsPerPage = 0
disjunctiveQuery.attributesToRetrieve = []
disjunctiveQuery.attributesToHighlight = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,5 +382,67 @@ class DisjunctiveFacetingHelperTests: XCTestCase {
])
XCTAssertTrue(response.exhaustiveFacetsCount!)
}

func testKeepExistingFilters() throws {
var query = Query()
query.filters = "NOT color:blue"

let refinements: [Attribute: [String]] = [
"size": ["m", "s"],
"color": ["blue", "green", "red"],
"brand": ["apple", "samsung", "sony"]
]
let disjunctiveFacets: Set<Attribute> = [
"color",
"size"
]
let helper = DisjunctiveFacetingHelper(query: query,
refinements: refinements,
disjunctiveFacets: disjunctiveFacets)
let queries = helper.makeQueries()
XCTAssertEqual(queries.count, 3)
XCTAssertEqual(queries.first?.filters, """
NOT color:blue AND ("brand":"apple" AND "brand":"samsung" AND "brand":"sony") AND ("color":"blue" OR "color":"green" OR "color":"red") AND ("size":"m" OR "size":"s")
""")
XCTAssertEqual(queries[1].facets, ["color"])
XCTAssertEqual(queries[1].filters, """
NOT color:blue AND ("brand":"apple" AND "brand":"samsung" AND "brand":"sony") AND ("size":"m" OR "size":"s")
""")
XCTAssertEqual(queries[2].facets, ["size"])
XCTAssertEqual(queries[2].filters, """
NOT color:blue AND ("brand":"apple" AND "brand":"samsung" AND "brand":"sony") AND ("color":"blue" OR "color":"green" OR "color":"red")
""")
}

func testKeepExistingFiltersEmpty() throws {
var query = Query()
query.filters = ""

let refinements: [Attribute: [String]] = [
"size": ["m", "s"],
"color": ["blue", "green", "red"],
"brand": ["apple", "samsung", "sony"]
]
let disjunctiveFacets: Set<Attribute> = [
"color",
"size"
]
let helper = DisjunctiveFacetingHelper(query: query,
refinements: refinements,
disjunctiveFacets: disjunctiveFacets)
let queries = helper.makeQueries()
XCTAssertEqual(queries.count, 3)
XCTAssertEqual(queries.first?.filters, """
("brand":"apple" AND "brand":"samsung" AND "brand":"sony") AND ("color":"blue" OR "color":"green" OR "color":"red") AND ("size":"m" OR "size":"s")
""")
XCTAssertEqual(queries[1].facets, ["color"])
XCTAssertEqual(queries[1].filters, """
("brand":"apple" AND "brand":"samsung" AND "brand":"sony") AND ("size":"m" OR "size":"s")
""")
XCTAssertEqual(queries[2].facets, ["size"])
XCTAssertEqual(queries[2].filters, """
("brand":"apple" AND "brand":"samsung" AND "brand":"sony") AND ("color":"blue" OR "color":"green" OR "color":"red")
""")
}

}

0 comments on commit 3ecb3ec

Please sign in to comment.