How to create relative search

so I want to create relative search in my app

let me explain

so this is my database and when I look for a book I don’t want to get the book name in search but the city in which the book is.

in simple language I search for books
48 laws of power in this case

and results should be the cities
kolhapur and pune in this case

How can I achieve this?

You need to restructure the data to accommodate your search. Using an object you can assign each book title as a property name and assign that property a list of the cities that are associated w/ the book title.




This is the JSON I simulated from your images:
{“city”: {“kolhapur”:{“library1”:[“48 laws of power:”,“founder’s office:”]},“mumbai”:{“library2”:[“founder’s office:”,“the motive:”]},“pune”:{“library3”:[“48 laws of power:”,“the motive:”]}}}

hey can you check my blocks
I feel I have done something wrong here



please let me know what might be the issue here

I think in this case I translated the JSON wrong.

I refactored the JSON to use the following:
{“city”: {“kolhapur”:{“library1”:{“48 laws of power”:,“founder’s office”:}},“mumbai”:{“library2”:{“founder’s office”:,“the motive”:}},“pune”:{“library3”:{“48 laws of power”:,“the motive”:}}}}

After updating the JSON I got the null value as well, and then I modified the blocks to get the object properties and it worked again.

It still seem to show null even after adding the get object properties block
do you think it might have something to do with my firebase database settings?

@itsvedanttikareztlgx I don’t think the issue is w/ your firebase settings, but rather the data schema… I’ve not used firebase a lot, so I don’t know if this is normal for firebase, but the data structure seems to be off, or because I don’t see the full scope of the data I think it’s off, and I’m guessing based on the images you’ve provided.

The values you’re seeking are the object properties, and it’s as if none of your data is actually stored in the values.

I would expect the data to be structured more like:

{
	"city": [
		{
			"name":"kolhapur",
			"library": [
					"48 laws of power",
					"founder’s office"
				]
		},
		{
			"name":"mumbai",
			"library": [
				"founder’s office",
				"the motive"
			]
		},
		{
			"name":"pune",
			"library": [
				"48 laws of power",
				"founder’s office"
			]
		}
	]
}

With the structure above your code blocks become a lot easier to read and manage because now you can target the direct property name instead of trying to find the properties in the objects and hope it’s the right data you’re looking for.

I don’t know if you have full control over the data, and how it’s structured, but getting the data as values of the properties vs using the properties as values may be the first step to resolve these issues and future issues.

Using the restructured data schema I refactored the blocks just to show you how much easier it is to read and follow the logic when you can access your data values directly.

So the search feature works hurray!!
thanks a lot for the help

Now I need to figure out how can I restructure firebase database, because it worked when I imported your JSON to my firebase database. But there are multiple layers to what I’m trying to do.

so there will basically be 3 layers
City > library > books

now the way I want the app to work is, when you enter the app you choose the city you are in
let’s say Mumbai for now

so once that is choosen now user only accesses data in mumbai segment of the database and so
Mumbai > Libraries > books

later user searches for a book
and user gets a result of all the libraries that contain that book
and so say user looks for
the motive

Now every library that has “the motive” will show up in list
and when clicked on any one library

A profile page for that particular library opens.

from where user can either go through google maps link provided in it
OR
check out the catalogue of that library, and find other books the library has

the reason I’m telling the whole thing is so you can have better understanding of what I’m trying to do.

The format I gave you would work.

Maybe do a search and look up how to structure your firebase data.

Here’s a quick primer on JSON

{} Curly braces represent an object with named properties
Square brackets represent an array with numerically indexed properties. The index starts at 0.
: Colons separate “Key”:“Value” pairs, and need to be surrounded by double quotes

Strings are alphanumeric and must be surrounded by double quotes
Integers & Demicals are numbers and don’t require double quotes
Boolean are true or false and don’t require double quotes

{
	"property1": [
		"list item 1",
		"list item 2",
		"list item 3"
	],
	"property2": {
		"sub1propertyofproperty2": true,
		"sub2propertyofproperty2": 3.14,
		"sub3propertyofproperty2": [
			1,
			2,
			3,
			"four"
		]
	}
}

Okay will have to look up how exactly can I use the firebase’s low code database structuring, because I will later also have to create an independent system to upload city and library data. To make registering libraries on app easier.

If you are structuring JSON for use with Thunkable, I recommend asking ChatGPT or Microsoft Copilot to suggest a format for your data. I found that to be helpful when I was creating an ideal Firebase realtime database structure.

Hey, so based on your responses I created a new JSON with the help of Gemini AI. Now the JSON that Gemini created was the exact way the app would have it’s structure.

Now I need help understanding how can I create what we created here

based on the new JSON
(I have color coded specific things in doc to make it easier to read)

and also how I can adjust the blocks for search feature

Here is a working block pattern.

The key to setting these up, is that each time you move down a level in the JSON you have to create a loop to access each of the items.

In the blocks I setup, I’ve been isolating each new level as a “temp” variable that will be changed each time the loop iterates.

In your previous JSON examples the book titles were all lower case, in this one they’re Title Case. So I suggest changing the case of the book title when saving it to the booList object and converting the search term both to lowercase, so you can do an exact match.

What I was finding during testing was that Thunkable is looking for the exact match:
The Shadow of the Wind != the shadow of the wind

okay so if I just store book names in lower case, it’ll be easier as later at any point we can change the casing of the letters

To clarify, I would put your book titles in Title Case in your database, like in your most recent JSON example.

When using the book title as the object property name in the bookList var, I would use lowercase.

When the user clicks to find the book title, I would convert the users search term to lowercase.

Then you can do an exact match to find the bookList var object property that matches the book title.

Whatever you do, don’t make your data fit the app. Your app needs to be able to work with and manipulate your data.

You can Integrate ChatGPT for Complex Search in JSON, If you use ChatGPT, You have just to send JSON, with Some prompts of the Search Term or Similar ( with some prompt engineering ) and it will give the Outputs of the Search.
If you want to it with this way, DM me as I can personally help you with this.


Want to hire a Certified Thunkable Expert? Whoocoder | Thunkable Expert | Order Now On Fiver!!