danielberkompas/elasticsearch-elixir

Elasticsearch.put_document does not save to database

Opened this issue · 4 comments

Can't get data to show on kibana.

I tried to save my data like this, not sure if i'm even doing this correctly:
Elasticsearch.put_document(MyApp.ElasticsearchCluster, struct(User, attrs), "users")

I'm using elasticsearch and kibana as docker services on docker-compose.

My configuration is as follows:


  elasticsearch:
    image: 'elasticsearch:6.5.4'
    ports:
      - "9200:9200"
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
  kibana:
    image: 'kibana:6.5.4'
    ports:
      - "5601:5601"
    environment:
      - ELASTICSEARCH_URL=http://elasticsearch:9200

UPDATE:
I'm getting this error when I execute the command above
"Incorrect HTTP method for uri [/users/_doc] and method [PUT], allowed: [POST]"

UPDATE 2:
Did Elasticsearch.put_document(MyApp.ElasticsearchCluster, struct(User, attrs), "users/_doc")
Saving to database, but every subsequent put_document call just updates that one document in the index. How do i add new documents with different ids.

UPDATE 3:
Did Elasticsearch.put_document(MyApp.ElasticsearchCluster, %User{id: 1, ...}, "users")
Saves multiple documents and solved the problem.
Question is why does this struct require an id? I thought if the struct did not contain an id, elasticsearch will automatically generate one?

Can you share your implementation of Elasticsearch.Document for User?

defimpl Elasticsearch.Document, for: MaisieApi.Accounts.User do
  def id(user), do: user.id
  def routing(_), do: false
  def encode(user) do
    %{
        firstName: user.first_name,
        lastName: user.last_name,
        zip: user.zip,
        email: user.email,
        role: user.role
    }
  end
end

I think there may be a bug where this library is posting nil as the ID of the user, when it really should post nothing when the ID is nil. Elasticsearch might not automatically generate an ID for a record if you post a nil ID? Bears more investigation.

so just to play safe, i'll populate the id field with an actual id, ie the id returned from postgres or something