cloudant / java-cloudant

A Java client for Cloudant
Apache License 2.0
79 stars 68 forks source link

What is the best way of creating search index for this scenario ? #406

Closed ZaheerUdDeen closed 6 years ago

ZaheerUdDeen commented 6 years ago

Scenario. I have a document in the database which has thousands of item in productListas below. image All the object in array productListhas the same shape and same fields with different values. Now I want to search in the following way. 1.when a user write 'c' against Ingrediants field, the list will show all Ingrediants start with alphabet 'c'. 2..when a user write 'A' against brandName field, the list will show all brandName start with alphabet 'A'.

please give an example using this to search for it, either it is by -creating an index(json,text). -creating a Search index (design document). or -using views etc Note: I don't to create an index at runtime(I mean index could be define by Cloudant dashboard) I just want to query it, by this library in the application.

I have read the documentation's, I got the concepts. Now I want to implement it with the best approach. I will use this approach to handle all such scenarios in future.

Sorry if the question is stupid :)

thanks.

ZaheerUdDeen commented 6 years ago

Anyone can answer Please?

emlaver commented 6 years ago

Hi @ZaheerUdDeen, one of us will be able to answer this tomorrow. Thank you for understanding.

emlaver commented 6 years ago

@ZaheerUdDeen I believe the best way is using a view as I didn't find a clean solution using Cloudant Query or Search.
Here's an example using your point 1:

Create a view with each Ingrediants element as key. Something like:

function (doc) {
  if(doc.productList) {
    for(var prod in doc.productList) {
      emit(doc.productList[prod].Ingrediants, doc.productList[prod]);
    }
  }
}

Now you can query the view using startkey and endkey and loop through the rows:

List<ViewResponse.Row<String, JsonElement>> rows = db.getViewRequestBuilder("newddoc", "new-view")
    .newRequest(Key.Type.STRING, JsonElement.class)
    .startKey("c")
    .endKey("d")
    .build()
    .getResponse().getRows();

for(ViewResponse.Row row : rows) {
    // Do something with each key and value
    System.out.println(row.getKey());
    System.out.println(row.getValue());
}

You should also be able to use the same idea above for your point 2.

tomblench commented 6 years ago

closing - no further action