Query on Array of strings to get matched values from Datastore - go

I have following scenario where struct is
type Band struct {
Name string `json:"name"`
Albums []String `json:"album"`
GradeLevel []string `json:"gradeLevel,omitempty"`
Topics []string `json:"topics,omitempty"`
}
Data stored like (Sample)
**Name Albums GradeLevel Topics**
Sample ["sample","Test"] ["grade1"] ["Children","Poems"]
test ["Test"] ["grade2","grade1"] ["therapy","slow"]
Here how to query to get appropriate values from Band kind with given inputs like
Request to query is {"album" : ["sample","Test"] , "gradeLevel" : ["grade1"] , "topic" : ["poem"]}
With combination of inputs are possible from front-end so how to query based input to display list of values for above scenario.

You can find information on writing queries in Go to retrieve data from Firestore in Datastore mode here [1]. To query if an array contains a value, you need to use an equality filter [2].
[1] https://cloud.google.com/datastore/docs/concepts/queries
[2] https://cloud.google.com/datastore/docs/concepts/queries#array_values

Related

How to search on a many-to-many field

Let's assume I have the below structs
type Job struct {
ID string `sql:"type:uuid;primary_key;"`
Title string `json:"title,omitempty"`
Skills []*skill.Skill `json:"skills,omitempty"gorm:"many2many:job_skill;"`
}
type Skill struct {
Name string `json:"name" gorm:"primary_key"`
}
To query all the jobs I do:
jobs := &[]Job{}
gorm.DB.Preload("Skills").Find(&jobs)
How do I search for a Job that contains a certain skill? I have tried the below but it says the column does not exist.
s := "golang"
jobs := &[]Job{}
gorm.DB.Preload("Skills").Where("skill = ?", s).Find(&jobs)
I can see my issues, = doesn't seem to be the correct operator as it needs to search in a list. And it also isn't loading the join table as I assumed it would
Debug output
pq: column "skill" does not exist
SELECT * FROM "jobs" WHERE skill = 'golang'
The Preload method and the Associations feature help you load your fields by constructing basic SQL queries based on the relationships you have created. Queries like loading all skills for a specific job (or jobs). But it doesn't go any more complex than that.
Rather, think of go-gorm as a way to construct your SQL queries and load the data into your models.
Having that in mind, one solution would be to use Joins to include the skill table.
gorm.DB.Preload("Skills")
.Joins("INNER JOIN job_skill js ON js.job_id = jobs.id").
.Joins("INNER JOIN skill s ON s.id = js.skill_id").
.Where("s.name = ?", s).Find(&jobs)

GraphQL ID type correction - how to use String?

I have a model with unique id's like d192a9be-c1f8-4099-8e88-7333ca07866d
When queried using graphql the result is id: "0" so it looks like it's trying to convert to Int.
How do I use String unique ID?

Elasticsearch query not returning expected results for multiple should filters

I am performing an Elasticsearch query using the high-level-rest-api for Java and expect to see records that are either active or do not have a reference id. I'm querying by name for the records and if I hit the index directly with /_search?q=, I see the results I want.
Is my logic correct (pseudo-code):
postFilters.MUST {
Should {
MustNotExist {referenceId}
Must {status = Active}
}
Should {
MustNotExist {referenceId}
Must {type = Person}
}
}
What I get are records that are active with a reference id. But, I want to include records that also do not have a referenceId, hence why I have MustNotExist {referenceId}.
For simplicity, the second Should clause can be dropped (for testing) as the first one is not working as expected by itself.
In my case, I had to use a match query instead of a term query because the value I was querying for was not a primitive or a String. For example, the part where Must, type = Person, Person was an enum, and so looking for "Person" was not quite right, whereas match allowed it to "match".

JHipster - Elasticsearch examples for search field

Having an example entity like
entity Vehicle {
id Long,
manufacturer String,
model String
}
and using ElasticSearch, how do queries in the search input field have to look like in the front end provided by the JHipster application?
A search might involve only one column or a combination of the given columns.
Let's say:
id = 1
manufacturer = Ferrari
manufacturer = Ferrari AND model = Model 1
or involving other query keywords like LIKE, etc.
How to specify those queries into the search input field?

Serialize to JSON dynamic structure

All examples of working with JSON describe how to serialize to JSON simple or user types (like a struct).
But I have a different case: a) I don't know the fields of my type/object b) every object will have different types.
Here is my case in pseudocode:
while `select * from item` do
while `select fieldname, fieldvalue from fields where fields.itemid = item.id` do
...
For each entity in my database I get field names and field values. In the result I need to get something like this:
{
"item.field1": value,
...
"item.fieldN": value,
"custom_fields": {
"fields.field1": value,
...
"fields.fieldK": value
}
}
What is the best way to do it in Go? Is there any useful libraries or functions in standard library ?
Update: The source of data is the database. In the result i need to get JSON as string to POST it to external web service. So, the program just read data from database and make POST requests to REST service.
What exactly is your target type supposed to be? It can't be a struct since you do not know the fields beforehand.
The only fitting type to me seems to be a map of type map[string]interface{}: with it any nested structure can be achieved:
a := map[string]interface{}{
"item.field1": "val1",
"item.field2": "val2",
"item.fieldN": "valN",
"custom_fields": map[string]interface{}{
"fields.field1": "cval1",
"fields.field2": "cval2",
},
}
b, err := json.Marshal(a)
See playground sample here.
Filling this structure from a database as you hinted at should probably be a custom script (not using json).
Note: custom_fields can also be of other types depending on what type the value column is in the database. If the value column is a string use map[string]string.

Resources