Last active
March 16, 2017 09:41
-
-
Save banaszeknorbert/9d6042ea101d141eb7554fe3a4b96f29 to your computer and use it in GitHub Desktop.
Query for Flashlight and ElasticSearch [Swift]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BodyQuery { | |
var index: String = "firebase" | |
var type: String = "product" | |
var size: String = "20" | |
var from: String = "0" | |
var request: [String: Any?] = [:] | |
var body: [String: Any?] = [:] | |
var body_query: [String: Any?] = [:] | |
var query: [String: Any?] = [:] | |
var bool: [String: Any?] = [:] | |
var filter: [Dictionary<String, Dictionary<String, Any?>>] = [] | |
var must: [Dictionary<String, Dictionary<String, Any?>>] = [] | |
var must_not: [Dictionary<String, Dictionary<String, Any?>>] = [] | |
var should: [Dictionary<String, Dictionary<String, Any?>>] = [] | |
var sort = [Dictionary<String, Any?>]() | |
func setIndex(index: String) { | |
request.updateValue(index, forKey: "index") | |
self.index = index | |
} | |
func setType(type: String) { | |
request.updateValue(type, forKey: "type") | |
self.type = type | |
} | |
func setFrom(from: String) { | |
request.updateValue(from, forKey: "from") | |
self.from = from | |
} | |
func setSize(size: String) { | |
request.updateValue(size, forKey: "size") | |
self.size = size | |
} | |
func mustMatch(key: String, value: String) { | |
var matching: [String: Any?] = [:] | |
var map: [String: Dictionary<String, Any?>] = [:] | |
matching.updateValue(value, forKey: key) | |
map.updateValue(matching, forKey: "match") | |
must.append(map) | |
} | |
func mustTerm(key: String, value: String) { | |
var matching: [String: Any?] = [:] | |
var map: [String: Dictionary<String, Any?>] = [:] | |
matching.updateValue(value, forKey: key) | |
map.updateValue(matching, forKey: "term") | |
must.append(map) | |
} | |
func mustNotTerm(key: String, value: String) { | |
var matching: [String: Any?] = [:] | |
var map: [String: Dictionary<String, Any?>] = [:] | |
matching.updateValue(value, forKey: key) | |
map.updateValue(matching, forKey: "term") | |
must_not.append(map) | |
} | |
func filterByMatchPhasePrefix(key: String, value: String) { | |
var sorting: [String: Any?] = [:] | |
sorting.updateValue(value, forKey: key) | |
sort.append(sorting) | |
} | |
func sort(key: String, value: String) { | |
var matching: [String: Any?] = [:] | |
var map: [String: Dictionary<String, Any?>] = [:] | |
matching.updateValue(value, forKey: key) | |
map.updateValue(matching, forKey: "sort") | |
sort.append(matching) | |
} | |
func returnSpecifiedFields(specifiedFields: Array<String>) { | |
body.updateValue(specifiedFields, forKey: "_source") | |
} | |
func matchAllWithTerms(key: String, termsList: Array<String>) { | |
var boost: Dictionary<String, Any?> = [:] | |
let boostValue: Double = 1.0 | |
boost.updateValue(boostValue, forKey: "boost") | |
var match_all: Dictionary<String, Dictionary<String, Any?>> = [:] | |
match_all.updateValue(boost, forKey: "match_all") | |
var terms: Dictionary<String, Dictionary<String, Array<String>>> = [:] | |
var values = [String: Array<String>]() | |
values.updateValue(termsList, forKey: key) | |
terms.updateValue(values, forKey: "terms") | |
body_query.updateValue(boost, forKey: "match_all") | |
body.updateValue(terms, forKey: "filter") | |
} | |
func matchAll() { | |
var boost = [String: Double]() | |
let boostValue: Double = 1.0 | |
boost.updateValue(boostValue, forKey: "boost") | |
body_query.updateValue(boost, forKey: "match_all") | |
} | |
func getQuery() -> [String : Any?] { | |
if (request["index"] == nil) { | |
request.updateValue("firebase", forKey: "index") | |
print("Type index is missing") | |
} | |
if (request["type"] == nil) { | |
request.updateValue("product", forKey: "type") | |
print("Type field is missing.") | |
} | |
if (!must.isEmpty) { | |
bool.updateValue(must, forKey: "must") | |
} | |
if (!filter.isEmpty) { | |
bool.updateValue(filter, forKey: "filter") | |
} | |
if (!must_not.isEmpty) { | |
bool.updateValue(must_not, forKey: "must_not") | |
} | |
if (!should.isEmpty) { | |
bool.updateValue(should, forKey: "should") | |
} | |
if (!bool.isEmpty) { | |
body_query.updateValue(bool, forKey: "bool") | |
} | |
if (!body_query.isEmpty) { | |
body.updateValue(body_query, forKey: "query") | |
} | |
if (!sort.isEmpty) { | |
body.updateValue(sort, forKey: "sort") | |
} | |
if (!body.isEmpty) { | |
query.updateValue(body, forKey: "body") | |
} | |
request.updateValue(query, forKey: "query") | |
return request | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Usage { | |
func createBodyQueryForMostPopularProducts() -> BodyQuery { | |
var fields: Array<String> = Array() | |
let query: BodyQuery = BodyQuery() | |
query.setIndex(index: "your_index") | |
query.setType(type: "your_type") | |
query.setSize(size: "size") | |
query.mustMatch(key: "key", value: "value") | |
query.sort(key: "key", value: "value") | |
query.returnSpecifiedFields(specifiedFields: fields) | |
return query | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment