dqjo4340 2018-01-06 04:27
浏览 25

根据golang中的结构值搜索记录

I am writing a service for search functionality. when i am passing values in the body to get the specific record i am able to get it based on the struct value only for PHONE.

I am very new to golang.

i need to search the values using all the fields in the struct for ex.phone or firstname or lastname of the patientstruct

my struct is as below

type PatientEntity struct {
    ID        int64 
    FirstName string 
    LastName  string 
    Phone     string 
}

my code is

func SearchPatientsHandler(res http.ResponseWriter, req *http.Request) {
    patient := &PatientEntity{}
    if err := json.NewDecoder(req.Body).Decode(patient); err != nil {
        panic(err)
    }

    patients := []*PatientEntity{}  
    q := datastore.NewQuery(KINDPATIENT).Filter("FirstName =",patient.FirstName) 

    keys, err := q.GetAll(ctx, &patients)

    if err != nil {
        // Handle error
        return
    }

    json.NewEncoder(res).Encode(patients)
}

i need to search with all the values of struct .how can i resolve.

  • 写回答

1条回答 默认 最新

  • drkxgs9358 2018-01-06 09:32
    关注

    If I understand correctly, you're trying to combine filters with OR. According to the docs, this is not possible:

    Cloud Datastore currently only natively supports combining filters with the AND operator.

    There's a tracking issue for this, but it's been there for ages with no action.

    You can instead issue a separate query for each filter, then merge the results (query for firstname, then query for lastname, then query for phone. Finally: combine all returns results while removing duplicates).

    Something along the lines of (pseudocode, but that's the logic for an OR search):

    var results []Patients
    if firstname != "" {
      results = append(results, query("firstname = ", firstname)
    }
    if lastname != "" {
      results = append(results, query("lastname = ", lastname)
    }
    etc...
    removeDuplicates(results)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端