Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moebius.DocumentQuery return only one entry? #98

Open
madasebrof opened this issue Jun 7, 2017 · 3 comments
Open

Moebius.DocumentQuery return only one entry? #98

madasebrof opened this issue Jun 7, 2017 · 3 comments

Comments

@madasebrof
Copy link

Not sure if this is a bug or a feature request, but I'm wondering how to return a single entry. For example, I have a series of entries that are similar but just have different ids and "created_at" fields (e.g. like a log file). I only want to get the most recent one. I thought it would work like this:

db(:my_db)
   |> search(for: "/users/accounts", in: [:name])
   |> sort(:id, :desc)
   |> limit(1)
   |> Polo.Db.run

but this returns all entries that match "/users/accounts".

When I inserted it, I used:

db(:my_db)
   |> searchable([:name])
   |> Polo.Db.save(name: name, value: body)

Any help is most appreciated!

@robconery
Copy link
Owner

I think you should be able to tack on first:

db(:my_db)
   |> search(for: "/users/accounts", in: [:name])
   |> sort(:id, :desc)
   |> Polo.Db.first

This is missing from the docs... which I really need to update.

@madasebrof
Copy link
Author

Thanks for the speedy reply!

Looks like it doesn't work:

** (ArgumentError) parameters must be of length 0 for query %Postgrex.Query{columns: ["id", "body", "created_at", "updated_at"], name: "", param_formats: [], param_oids: [], param_types: [], ref: #Reference<0.0.1.10021>, result_formats: [:binary, :binary, :binary, :binary], result_oids: [23, 25, 1184, 1184], result_types: [Postgrex.Extensions.Int4, Postgrex.Extensions.Raw, Postgrex.Extensions.TimestampTZ, Postgrex.Extensions.TimestampTZ], statement: "select id, body::text, created_at, updated_at\nfrom quovo\n\n order by body -> 'id' desc\n\n;\n", types: {Moebius.PostgrexTypes, 639035}}
         (postgrex) lib/postgrex/query.ex:49: DBConnection.Query.Postgrex.Query.encode/3
    (db_connection) lib/db_connection.ex:1071: DBConnection.describe_run/5
    (db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:1133: DBConnection.run_meter/3
    (db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4
         (postgrex) lib/postgrex.ex:134: Postgrex.query/4
          (moebius) lib/moebius/database.ex:234: Moebius.Database.execute/1
             (polo) lib/moebius/database.ex:185: Polo.Db.execute/1
             (polo) lib/moebius/database.ex:73: Polo.Db.first/1
             (polo) lib/polo/quovo/user_mgmt.ex:13: Polo.Quovo.UserMgmt.show_accounts/1

Also, it looks like the sort function translates to body -> 'id' as opposed to just plain id. Thus, it looks like you wouldn't be able to sort based on id created_at or updated_at anyway.

Oh well! :)

Thanks anyway, maybe I can figure out a workaround that doesn't involve returning all entries of a given key and sorting it after the fact...

Ideally, what I'd want is a Db.tail function that returned the last entry by either getting the maximum id created_at or updated_at.

db(:my_db)
   |> search(for: "/users/accounts", in: [:name])
   |> tail(:id)
   |> Polo.Db.tail

I would imagine this would be a useful thing to have!

Best,

@robconery
Copy link
Owner

Sorry for the delay on this for some reason I wasn't notified of your response :(. Yes, definitely useful and I don't have time to dig in currently but would welcome any help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants