meilisearch / meilisearch-java

Java client for Meilisearch
https://meilisearch.com
MIT License
186 stars 113 forks source link

The addDocuments and updateDocuments methods should take a primary key as optional parameter #78

Closed curquiza closed 3 years ago

curquiza commented 3 years ago

The addDocument and updateDocuments methods do not take any primary key as parameter, but they should. Related docs:

⚠️ The primary key parameter should be optional!

JohanDelValleV commented 3 years ago

Hey, It's me again, can I take this issue? @curquiza

curquiza commented 3 years ago

Hello again! 😄 Of course you can! I assign you!

curquiza commented 3 years ago

Also, can you add the same to updateDocuments? So sorry for this addition. I've updated the issue description and title.

JohanDelValleV commented 3 years ago

Ok, no problem. Can you explain to me more about how to pass that param or how to overwrite the primaryKey? And if you do not mind giving a query example

curquiza commented 3 years ago

This is useful to pass the primary key as a query parameter the first time you add or update documents if the primary key has not been inferred or set before.

If you want to pass the primary key, just pass it as a query parameter:

$ curl
  -X POST 'http://localhost:7700/indexes/movies/documents?primaryKey=id
  --data '[{
      "id": 287947,
      "title": "Shazam",
      "poster": "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg",
      "overview": "A boy is given the ability to become an adult superhero in times of need with a single magic word.",
      "release_date": "2019-03-23"
  }]'

If the primary-key has been already set/inferred, you can still pass it as a query parameter but the parameter will be just ignored. It's maybe not the perfect way to handle it, but MeiliSearch does not handle warnings for the moment. So you don't need to check a primary key exists: just pass it to the MeiliSearch server, it will handle it.

JohanDelValleV commented 3 years ago

Got it. But, I just testing and every time I made your example, it needs to match the query parameter with the id name something like your example, but if I do this:

  $ curl
    -X POST 'http://localhost:7700/indexes/movies/documents?primaryKey=id
      --data '[{
              "movie_id": 287947,
              "title": "Shazam",
      }]'

Notice that the query parameter is different from the data. The document won't be created. So, if you have an idea of how to start this issue, let me know.

curquiza commented 3 years ago

In your example, because the primary key is id, the document is not created because MeiliSearch does not find any id field in the document. For MeiliSearch, there is no primary key and it cannot add a document without it. So you don't have to care about this when doing this issue (add a parameter to both methods). This is how MeiliSearch works, not the Java client.

With your example, when getting the update status with this method, can you confirm the error you can see? More about the document addition response -> https://docs.meilisearch.com/faq/#i-did-a-call-to-an-api-route-and-i-only-got-an-updateid-as-a-response-what-does-it-mean

OmarMorales71 commented 3 years ago

@curquiza Hello can I work on this issue? I'm investigating how these methods work.

curquiza commented 3 years ago

Hello @OmarMorales71! Thanks for your involvement! @JohanDelValleV seemed to want to do this issue: https://github.com/meilisearch/meilisearch-java/issues/78#issuecomment-742761443 . Maybe should we wait for a little and ask him before starting it?

However, if you want to do an issue, I've just opened this one #93 which is really important and useful for this repo 😄

vishnugt commented 3 years ago

Since this issue was inactive for more than 10 months, I took the liberty to send a PR for this, hope it's okay. @curquiza