elastic_client icon indicating copy to clipboard operation
elastic_client copied to clipboard

Cannot get value from second field from search result

Open alexandroheredia opened this issue 5 years ago • 0 comments

I can search for a document by one field and get results just fine, but how can I get the values from the other fields that specific document has?

My expected result:

ListTile(
   title: Text(foodName),
   subtitle: Text(brandName),
);

This is my working code (you're free to add it as a real-world-example using Elastic Cloud in this repo if you want):

import 'package:elastic_client/elastic_client.dart';
import 'package:flutter/material.dart';
import 'package:elastic_client/elastic_client.dart' as elastic;

class ElasticSearchDelegate extends SearchDelegate {

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      IconButton(
        icon: Icon(Icons.clear),
        onPressed: () async {
          query = '';
          await searchElasticServer(query);
        },
      ),
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      icon: Icon(Icons.arrow_back),
      onPressed: () {
        close(context, null);
      },
    );
  }

  Future searchElasticServer(searchQuery) async {
    final transport = HttpTransport(
      url: 'ELASTIC_SEARCH_ENDPOINT_URL',
      authorization: basicAuthorization("ELASTIC_SEARCH_USERNAME", "ELASTIC_SEARCH_PASSWORD"));
    final client = elastic.Client(transport);

    List<String> foodsList = List();

    final searchResult = await client.search(
        index: 'foods', 
        type: '_doc', 
        query: elastic.Query.term('foodName', ['$searchQuery']),
        source: true);

    print("----------- Found ${searchResult.totalCount} $searchQuery ----------");

    for(final iter in searchResult.hits){
      Map<dynamic, dynamic> currDoc = iter.doc;

      print(currDoc);

      foodsList.add(
        currDoc['foodName'].toString(),
        );
    }

    await transport.close();

    if(searchResult.totalCount <= 0 )
      return null;
    else
      return foodsList;
  }

  @override
  Widget buildResults(BuildContext context) {
    return FutureBuilder(
      future: searchElasticServer(query),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) return Center(child: Text("No results"));

        return  _displayFoodResult(snapshot.data) ;
      },
    );
  }

  @override
  Widget buildSuggestions(BuildContext context) {

    return FutureBuilder(
      future: searchElasticServer(query),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) return Text("No results");

        return _displayFoodResult(snapshot.data) ;
      },
    );
  }


  Widget _displayFoodResult( List<String> foodList) {
    return ListView.builder(
      itemCount: foodList.length,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          title: Text(foodList[index]),
        );
      }
    );
  }

}

Result on screen: image

Debug Console result: image

alexandroheredia avatar Jun 01 '21 12:06 alexandroheredia