new_in_town
01/19/2025, 8:18 PM"countries":["CA","IN","US"]
Typesense can index this field and can search it, this works.
Now I want to index some additional info: state_or_province. The idea is to allow Typesense search using:
1. country
2. country + state_or_province, something like this: "USA", "CA"
I am looking for the optimal JSON data structure for each document to store:
- country codes
- for each country code: list of state_or_province, optional
This should be something that almost everybody needs (and there is more than one way to do it in JSON)Jason Bosco
01/20/2025, 2:34 AMRob Bast
01/20/2025, 7:46 AMRob Bast
01/20/2025, 7:47 AMTip: it works best when we separate the address and zipcode into their own field and settingTalking about this note 😅in the search params.query_by: adddress_without_zipcode, zipcode
Fanis Tharropoulos
01/20/2025, 8:44 AMThe documentation on the website is not in sync with the documentation in the repo?Are you talking about the markdown format not being in sync with what is rendered in the documentation site, or the tip link not pointing to the same part in the codebase?
Rob Bast
01/20/2025, 9:02 AMnew_in_town
01/20/2025, 6:20 PMHere's an address search demo that shows you how to best structure the data for this use case: https://typesense.org/docs/guide/reference-implementations/address-autocomplete.htmlThat's a great demo, but I am talking about different data format! Instead of this demo dataset containing postcodes:
...
{"postcode":"02215","address":"8 Aberdeen Street, Boston, MA"}
{"postcode":"02108","address":"1 Acorn Street, Boston, MA"}
{"postcode":"02108","address":"2 Acorn Street, Boston, MA"}
...
I want to search (Typesense search!) in the dataset as follows:
...
"US": {
"states": [
"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
"MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
"SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"
]
},
"CA": {
"states": [
"AB", "BC", "MB", "NB", "NL", "NS", "NT", "NU", "ON", "PE",
"QC", "SK", "YT"
]
},
"DE": {
"states": [
"BW", "BY", "BE", "BB", "HB", "HH", "HE", "NI", "MV",
"NW", "RP", "SL", "SN", "ST", "SH", "TH"
]
},
...
new_in_town
01/20/2025, 6:22 PMnew_in_town
01/20/2025, 6:24 PMnew_in_town
01/20/2025, 6:24 PMnew_in_town
01/20/2025, 6:37 PMJason Bosco
01/20/2025, 9:10 PM{"country": "US", "state": "AL"},
{"country": "US", "state": "AK"},
{"country": "US", "state": "AZ"},
...
{"country": "CA", "state": "AB"},
{"country": "CA", "state": "BC"},
{"country": "CA", "state": "MB"},
...
{"country": "DE", "state": "BW"},
{"country": "DE", "state": "BY"},
{"country": "DE", "state": "BE"}
To show all the countries, you'd do facet_by: country
. And then when a country is selected (eg: US), you'd change the query to filter_by: country:=US , facet_by: state
to get all the states for the selected country