1

I have Spring Boot + DocumentDB application in which we need to implement a search API, The Search fields are part of nested Json are as below:

{
  "id":"asdf123a",
  "name":"XYZ",
  add{
      "city":"ABC"
      "pin":123456,  
     }
 } 

I need to search with name="XYZ" and city="ABC", I'm trying with below code but somehow not able to retrieve the record. But I'm able to retrieve with just by Name or ID, but not with name and city or just city which is part of nested JSON.

Employee{
private String id;
private String name;
private Address add
/*Getter and Setters {} */
}

Address{
private String city;
private Long pin;
/*Getter and Setters {} */
}

public class EmployeeController {
EmployeeRepository repository;

@Autowire
EmployeeController (EmployeeRepository repository){
this.repository = repository;
}

@GetMapping(value = "/search", produces = APPLICATION_JSON_VALUE_WITH_UTF8)
public ResponseEntity<?> Search (@RequestParam ("name")String name, 
                                 @RequestParam("city") String city){    

 return new ResponseEntity <> (repository
                           .findByNameLikeAndAddressCityLike(
                                   name, city), 
                                 HttpStatus.OK
                          );
 }
}

@Repository
public interface EmployeeRepository extends DocumentDbRepository<Employee, 
String> {

Optional<Employee>findByNameLike(String name); // Perfectly working

Optional<Employee>findByAddressCityLike(String city); // Not working 

Optional<Employee>findByNameLikeAndAddressCityLike(String name, String 
city); // Not Working

}

Also Just like Spring JPA we use @Query to fire custom/ native query are there any DocumentDB Annotation present if so please guide me with example or Docuemnt. Looking for help

Manjunath
  • 35
  • 6
  • Do you have indexing turned on for the "city" field? If not, it won't find it unless you enable "scan" in the request options (which I would not recommend because it'll be WAY slower, best to add the index). – Chris Anderson Sep 27 '18 at 22:12
  • @ChrisAnderson-MSFT The https://learn.microsoft.com/en-us/azure/cosmos-db/indexing-policies says All properties are by default Indexed . in exact wording -- By default, Azure Cosmos DB indexes all string properties within documents consistently with a Hash index. It indexes all numeric properties within documents consistently with a Range index. Do you think I'm still missing something.? – Manjunath Sep 28 '18 at 07:43

0 Answers0