Hi all, I am importing into Typesense some docume...
# community-help
n
Hi all, I am importing into Typesense some documents with country/state information. At the moment: these documents have "countries" field, something like this (2-letter-country-codes):
"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)
j
Here'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.html
r
The documentation on the website is not in sync with the documentation in the repo?
Tip: it works best when we separate the address and zipcode into their own field and setting
query_by: adddress_without_zipcode, zipcode
in the search params.
Talking about this note 😅
f
The 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?
r
just that the query by mentioned there references a field that does not exist in the repository
n
Here'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.html
That'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"
]
},
...
CA = Canada, DE = Germany etc... UI should contain two drop-down lists: Select country: US, CA, DE... Select state:
The question is: how to import this data into Typesense and how to search? I am non insisting on the JSON above: it can be transformed to fit into Typesense...
@Jason Bosco what would you suggest?
P.S. Not always states can be two-letter-codes
j
I see, given your UI requirements, it would be better to transform the data like this, where each row is a different document in Typesense:
Copy code
{"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