Using API & Dart to filter multiple conditions?

Hello,

I am looking to create an advanced filter in my flutter project.
Currently, I can filter on Gender and player scores to find the best players in that Gender.

I am trying to then add the option to be able to filter those players to what role they play as and where they are in the world.
I store this data as the following in algolai
Gender
role
_geoloc (latlng)
playeravg

My code is as follows:
Future filterquery(
double? playeravg,
double? awarenessavg,
double? strengthavg,
double? abilityavg,
double? paceavg,
String? Gender,
String? role,
LatLng? location,
) async {
) async {
final client = SearchClient(
appId: ‘’,
apiKey: ‘’,
);

final queryHits = SearchForHits(
indexName: ‘users’,
query: ‘$Gender’,
numericFilters: [
‘playeravg >= $playeravg’,
‘awarenessavg >= $awarenessavg’,
‘strengthavg >= $strengthavg’,
‘abilityavg >= $abilityavg’,
‘paceavg >= $paceavg’,
],
);

Using AND on query returns an error and filters in the documentation seem to only allow AND OR NOT for my strings.

Currently, I am then restricted to only being able to filter on Gender and scores. Which is all working.

I just want to build out the functionality now to also be able to filter on role and location (with in an input radius from a variable if possible)

Any suggestions?

Hello,

To enhance your Algolia search query in Flutter to include filtering by “role” and location (within a radius), follow these steps:

Indexing Setup: Ensure your Algolia index (users) includes attributes role (as a facet) and _geoloc (for location).

Adjust Search Query: Modify your Flutter code to construct a comprehensive Algolia query:

import 'package:algolia/algolia.dart';

Future<List<AlgoliaObjectSnapshot>> filterQuery(
  double? playeravg,
  double? awarenessavg,
  double? strengthavg,
  double? abilityavg,
  double? paceavg,
  String? Gender,
  String? role,
  LatLng? location,
  double? radiusInMeters,
) async {
  final client = Algolia.init(
    applicationId: 'YOUR_APPLICATION_ID',
    apiKey: 'YOUR_API_KEY',
  );

  final query = Query(indexName: 'users')
    ..numericFilters = [
      'playeravg >= $playeravg',
      'awarenessavg >= $awarenessavg',
      'strengthavg >= $strengthavg',
      'abilityavg >= $abilityavg',
      'paceavg >= $paceavg',
    ];

  if (Gender != null) {
    query.filters = 'Gender:$Gender';
  }
  if (role != null) {
    query.filters += ' AND role:$role';
  }

  if (location != null && radiusInMeters != null) {
    query.aroundLatLng = LatLng(location.latitude, location.longitude);
    query.aroundRadius = AlgoliaQueryRadius(radius: radiusInMeters);
  }

  final result = await client.instance.index('users').query(query).getObjects();

  return result.hits;
}

Explanation:
Numeric Filters: Filter numeric attributes like playeravg, awarenessavg, etc., for fine-tuning player scores.

String Filters: Use query.filters to apply filters for Gender and role, combining them with AND for simultaneous application.

Location-Based Filtering: Utilize aroundLatLng and aroundRadius to filter by geographical coordinates (_geoloc) within a specified radius in meters.

Ensure your Algolia index is configured correctly with facets and geographic attributes for accurate filtering. This streamlined approach enables advanced filtering capabilities in your Flutter app using Algolia. Official Site

Hope this will work for you.
Best Regard,
florence023