Fix settings by making key filterable in meilisearc

This commit is contained in:
2025-05-25 10:05:48 +02:00
parent d78db03d5d
commit 27f21a23fe
3 changed files with 22 additions and 23 deletions

View File

@@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:journaler/db.dart';
import 'package:journaler/notes.dart';
import 'package:journaler/utils.dart';
import 'package:system_tray/system_tray.dart';

View File

@@ -97,39 +97,34 @@ Future<List<Map<String, dynamic>>> getAllDocuments(String index) async {
final headers = await _getHeaders();
final allDocuments = <Map<String, dynamic>>[];
var offset = 0;
const limit = 100; // Process in batches of 100
const limit = 100;
while (true) {
final searchCondition = MeilisearchQuery(
q: '',
limit: limit,
offset: offset,
);
final response = await http.post(
Uri.parse('$endpoint/indexes/$index/search'),
final response = await http.get(
Uri.parse('$endpoint/indexes/$index/documents?limit=$limit&offset=$offset'),
headers: headers,
body: jsonEncode(searchCondition.toJson()),
);
if (response.statusCode != 200) {
throw Exception('Failed to get documents: ${response.statusCode}');
}
final responseJson = MeilisearchResponse.fromJson(
jsonDecode(response.body),
);
final hits = List<Map<String, dynamic>>.from(responseJson.hits);
if (hits.isEmpty) {
final responseJson = jsonDecode(response.body);
final documents = List<Map<String, dynamic>>.from(responseJson['results']);
if (documents.isEmpty) {
break;
}
allDocuments.addAll(hits);
allDocuments.addAll(documents);
print('Found ${allDocuments.length} documents so far in $index');
if (documents.length < limit) {
break;
}
offset += limit;
if (hits.length < limit) {
break;
}
}
print('Total documents found in $index: ${allDocuments.length}');
return allDocuments;
}

View File

@@ -142,6 +142,11 @@ Future<void> init() async {
body: jsonEncode({'uid': settingsIndex, 'primaryKey': 'key'}),
);
}
await http.put(
Uri.parse('$endpoint/indexes/$settingsIndex/settings/filterable-attributes'),
headers: headers,
body: jsonEncode(['key', 'value']),
);
}
Future<bool> indexExists(String index) async {