zir_semantic_search 0.0.7 zir_semantic_search: ^0.0.7 copied to clipboard
Zir AI semantic search.
Flutter Semantic Search Plugin
This documentation demonstrates how to integrate the ZIR Semantic Search widget into a Flutter application.
For full 📄 documentation, visit the online documentation.
The search widget connects to a corpus or corpora through API keys. It presents the user with a polished customizable text box for entering queries, and handles results and errors using callback methods.
Usage #
If you haven't yet installed ZIR AI in your project, check the installation tab.
Create the application and import the ZIR AI library into the project:
import "package:zir_semantic_search/pagination/button.dart";
import "package:zir_semantic_search/pagination/pagination.dart";
import "package:zir_semantic_search/pagination/pagination_helpers.dart";
import "package:zir_semantic_search/search_box/search_box.dart";
void main() => runApp(InitialApp());
class InitialApp extends StatefulWidget {
@override
_InitialApp createState() => _InitialApp();
}
Next, initialize the ZIR AI Search Box widget:
class _InitialApp extends State<InitialApp> {
var results = new Map();
GlobalKey<SearchBox> _myKey = GlobalKey();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "My App",
home: Scaffold(
body: Center(
child: Column(
children: [
Padding(
padding: EdgeInsets.only(top: 30),
child: ZirSearchBox(
key: _myKey,
apiKey: 'zqt_cKg6-jmMWbiFViZw_6fEPl2JITLhXVk0jg3TQg',
corpusId: [234],
customerId: 1890073338,
image: "assets/images/logo.png",
results: 20,
offset: 0,
callback: (value) {
// print received respose
print(res);
setState(() {
results = value;
});
},
),
)
The ZIR AI Search Box widget has successfully been embedded into the project.
A brief description of each method parameter is below:
- key: initialize a
GlobalKey
instance and pass the value toZirSearchBox
- apiKey: the API key linked to the corpora to be queried.
- customerID: your account ID.
- corpusID: an array of IDs of the corpora to be queried. This can range
from a single corpus to an account-specific limit, which is generally five. - image: the ZIR logo is shown in the search box by default. This can be
altered by passing in the path to a replacement image. - results: the desired number of search results. ten results are returned by default.
To alter this, pass in any positive integer value up to the maximum number
allowed by the account. - offset: the number of records the database should skip before selecting
records. For example, if a value of 3 is passed, the first three results of
the result set will not be included. - callback: a callback function to handle the response. Use this function
to render individual results on the page.
Use the runSearch
method to run queries from outside the search box. The
GlobalKey
initialized earlier comes in handy.
_myKey.currentState!.runSearch("our query")
The snippet below exemplifies how this can be used. Queries are populated into
ActionChips
and onPressed
event handlers to each, so that the clicked query
is passed as an argument into the runSearch
method.
Container(
width: 400,
child: Row(
children: [
ActionChip(
label: Text("Thinking of watching a movie?"),
onPressed: () async {
final res = await _myKey.currentState!.runSearch("What movies are playing today?");
// print received respose
print(res);
setState(() {
results = res;
});
}),
ActionChip(
label: Text("How about some coffee?"),
onPressed: () async {
final res = await _myKey.currentState!.runSearch("What is the highest rated coffee shop near me?");
// print received respose
print(res);
setState(() {
results = res;
});
}),
],
),
),
Pagination #
Pass the GlobalKey
created earlier into the ZirPagination
component as
shown below:
ZirPagination(searchKey: _myKey1)
The Complete Example #
The complete implementation is below:
import "dart:convert";
import "package:flutter/material.dart";
import "package:zir_semantic_search/zir_semantic_search.dart";
import "./search_box.dart";
void main() => runApp(InitialApp());
class InitialApp extends StatefulWidget {
@override
_InitialApp createState() => _InitialApp();
}
class _InitialApp extends State<InitialApp> {
var results = new Map();
GlobalKey<SearchBox> _myKey = GlobalKey();
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "My App",
home: Scaffold(
body: Center(
child: Column(
children: [
Padding(
padding: EdgeInsets.only(top: 30),
child: ZirSearchBox(
key: _myKey,
apiKey: 'zqt_cKg6-jmMWbiFViZw_6fEPl2JITLhXVk0jg3TQg',
corpusId: [234],
customerId: 1890073338,
image: "assets/images/logo.png",
results: 20,
offset: 0,
callback: (value) {
// printing received respose
print(res);
setState(() {
results = value;
});
},
),
),
Container(
width: 400,
child: Row(
children: [
ActionChip(
label: Text("Thinking of watching a movie?"),
onPressed: () async {
final res = await _myKey.currentState!.runSearch("What movies are playing today?");
// print received respose
print(res);
setState(() {
results = res;
});
}),
ActionChip(
label: Text("How about some coffee?"),
onPressed: () async {
final res = await _myKey.currentState!.runSearch("What is the highest rated coffee shop near me?");
// print received respose
print(res);
setState(() {
results = res;
});
}),
],
),
),
SearchApp() // another widget using ZirSearchBox
],
),
),
),
);
}
}