Skip to content

Instantly share code, notes, and snippets.

@abhinavraj23
Created December 29, 2019 20:46
Show Gist options
  • Save abhinavraj23/28ef8e92f85d202f70e702589417a954 to your computer and use it in GitHub Desktop.
Save abhinavraj23/28ef8e92f85d202f70e702589417a954 to your computer and use it in GitHub Desktop.
getPopularSearches = value => {
var headers = new Headers();
//This the DSL query for searching according to given value
const data = {
"query": {
"bool": {
"must": [
{
"bool": {
"must": {
"bool": {
"should": [
{
"multi_match": {
"query": value,
"fields": [
"key"
],
"type": "best_fields",
"operator": "or",
"fuzziness": 0
}
},
{
"multi_match": {
"query": value,
"fields": [
"key"
],
"type": "phrase_prefix",
"operator": "or"
}
}
],
"minimum_should_match": "1"
}
}
}
}
]
}
},
"size": 20
}
headers.append('Authorization', 'Basic ' + btoa(USERNAME + ':' + PASSWORD));
headers.append('Content-Type', 'application/json');
fetch(ARC_CLUSTER_URL + "/.suggestions/_search",
{
method: 'POST',
body: JSON.stringify(data),
headers: headers
}).then(res => res.json())
.then(
(result) => {
console.log("result", result.hits.hits);
var src = result.hits.hits
var arr = [];
for (var i = 0; i < src.length; i++) {
arr.push(src[i]["_source"]["key"]);
}
//save the popular searches in the state
this.setState({
popular_searches: arr
})
},
(error) => {
console.log("Error is", error);
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment