basho / riak-erlang-client

The Riak client for Erlang.
Apache License 2.0
311 stars 188 forks source link

Searching data types - map support [JIRA: CLIENTS-1029] #332

Open mikrofusion opened 7 years ago

mikrofusion commented 7 years ago

The docs talk to the ability to use search with CRDTs. Their are counters & sets examples which for multiple languages, which works great. Unfortunately the examples on searching maps don't include erlang:

http://docs.basho.com/riak/kv/2.1.4/developing/usage/searching-data-types/#maps-example

Does that mean this client only supports the counter & set data types? If map is supported (hopefully it is), is there somewhere with examples that can be referenced? I attempted to follow the same pattern as searching counters & sets, but was unsuccessful.

Thanks for your help.

lukebakken commented 7 years ago

Can you provide the following?

mikrofusion commented 7 years ago

Hi @lukebakken - thanks for the response.

I am using the default Riak Search schema (_yz_default). I put it into a gist here: https://gist.github.com/mikrofusion/a084a7140f706243dbdd0a64f612990c

For some background, we are planning on using Riak + elixir for a large project at work. My spike on testing the riak-erlang-client + Riak + elixir is here: https://github.com/mikrofusion/elixir_riak

The spec that is failing is the following:

      context "searching maps" do
        before do
          :ok = :riakc_pb_socket.create_search_index(shared.pid, "map_search")
          :ok = :riakc_pb_socket.set_search_index(shared.pid, {"maps", "bucket"}, "map_search")

          map = :riakc_map.new()
          map = :riakc_map.update({"reg", :register},
            fn(r) -> :riakc_register.set("foo", r) end,
          map)

          :ok = :riakc_pb_socket.update_type(shared.pid, {"maps", "bucket"}, "key", :riakc_map.to_op(map))

          :timer.sleep(1000) # note: appears it takes some time to index
        end

        it "allows you to fetch the results" do
          # doesn't seem to work, looking for help here:
          # https://github.com/basho/riak-erlang-client/issues/332
          {:ok, {:search_results, results, _, _}} = :riakc_pb_socket.search(shared.pid, "map_search", "maps:*")

          expect Enum.count(results) |> to(eq 1)
        end
      end

Found here: https://github.com/mikrofusion/elixir_riak/blob/master/spec/test_spec.exs#L445

Similar specs above (with counters and sets) work fine.

Thanks again for all your help.

lukebakken commented 7 years ago

I will try to get to this today or tomorrow as time allows.

churcho commented 7 years ago

Any traction on this? I am facing the same issue with my implementation.

churcho commented 7 years ago

I think I found a way to make my search work.

Had to carefully read this section In there it says

To query embedded fields, you must provide the name of the field.

An example is given like :Maps containing a set called hobbies hobbies_set:*

In my case I have a users bucket of type CRDT maps with a register field called username which I am using for my search indexing.

def search_by_index(index, field_name, search_param) do
    :riakc_pb_socket.search(:pooler.take_group_member(:riak), index, "#{field_name}:#{search_param}", [])
  end

I am calling seach_by_index this way: search_by_index("email", "email_register","myemail@test.com")

That gives me the desired results.