Created
December 6, 2020 07:39
-
-
Save utkarsh-UK/70c25ed8fdeab05d6da94b5f986e8128 to your computer and use it in GitHub Desktop.
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
import 'package:dio/dio.dart'; | |
class ElasticRemoteDataSourceImpl { | |
final Dio dio; | |
ElasticRemoteDataSourceImpl(this.dio); | |
Future<List<String>> searchServices(String index, String query) async { | |
const String ELASTIC_BASE_URL = 'http://localhost:9200/'; //Change this to your publish_address | |
final Map<String, dynamic> body = { | |
'query': { | |
'query_string': { | |
'default_field': 'search_query', | |
'query': '*$query*', // pass the query from application | |
} | |
} | |
}; | |
final response = await dio.post( | |
ELASTIC_BASE_URL + '/$index/_search', //use the index | |
data: body, | |
options: Options( | |
headers: {'Content-Type': 'application/json'}, | |
), | |
); | |
if (response.statusCode == 200) { | |
List<String> services = []; | |
List<dynamic> hits = response.data['hits']['hits']; //extract the data into list | |
hits.forEach((service) => services.add(service)); //add to our services list and return | |
return services; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment