SetDistinct(true) Not Working

queryBuilder.setDistinct(true) is not working for me. I still get duplicate records in the returned response object.

C2AE98FF-1A1D-BF10-FF3E-64CD97C40D00
SDK version 6.2.1

Hi @Nkekere_Tommy_Minimann !

Could you please provide your query for which distinct didn’t work?

Regards, Andriy

    final String[] models = {""};
    DataQueryBuilder queryBuilder = DataQueryBuilder.create();

    queryBuilder.setDistinct(true);
    queryBuilder.setPageSize(100);
    Backendless.Data.of(Automotive.class).find(queryBuilder, new AsyncCallback<List<Automotive>>() {


        @Override
        public void handleResponse(List<Automotive> response) {

            for (int i = 0; i < response.size(); i++) {
                models[0] += response.get(i).getMake() + " " + response.get(i).getModel() + "#";

                Log.d(getClass().getName(), "Models:" + models[0]);
                //Am getting duplicates in the models[0] string

            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    });

Hello @Nkekere_Tommy_Minimann

Try to add properties which you wish get without duplicate:
queryBuilder.setProperties( new ArrayList<>( List.of( "Make", "Model" ) ) );

//I used this syntax
ArrayList properties = new ArrayList( Arrays.asList(“Make”,“Model”));

//But am still getting duplicates
//I even tried putting queryBuilder.setDistinct() //before and after . setProperties (), but it’s still //the same.

final String[] models = {""};
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
ArrayList properties = new ArrayList( Arrays.asList(“Make”,“Model”));

queryBuilder.setProperties(properties);
queryBuilder.setDistinct(true);
queryBuilder.setPageSize(100);
Backendless.Data.of(Automotive.class).find(queryBuilder, new AsyncCallback<List>() {

    @Override
    public void handleResponse(List<Automotive> response) {

        for (int i = 0; i < response.size(); i++) {
            models[0] += response.get(i).getMake() + " " + response.get(i).getModel() + "#";

            Log.d(getClass().getName(), "Models:" + models[0]);
            //Am getting duplicates in the models[0] string

        }
    }

    @Override
    public void handleFault(BackendlessFault fault) {

    }
});

Hi, @Nkekere_Tommy_Minimann

I was unable to reproduce your issue in both my app and your app. I have tried using classes and Map with enabled/disabled SetDistinct(true) option. Without distinct I got 39 objects in rosponse. Whrn distinct was enabled 28. SDK version I used for testing 6.2.1. My code:

   DataQueryBuilder queryBuilder = DataQueryBuilder.create();
    ArrayList properties = new ArrayList( Arrays.asList("Make","Model"));

    queryBuilder.setProperties(properties);
  //queryBuilder.setDistinct(true);
    queryBuilder.setPageSize(100);
    Backendless.Data.of(Automotive.class).find(queryBuilder, new AsyncCallback<List<Automotive>>() {

        @Override
        public void handleResponse(List <Automotive> response) {
            System.out.println(response.size());
            for (int i = 0; i < response.size(); i++) {
                System.out.println (response.get(i).getModel() + " " + response.get(i).getMake());

            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    });

Please, try to execute your code using Map. If you still able to reproduce the problem, please provide us with your full code.

Regards,
Marina

//I’ve tried using Map and i still get the same result (With setDistinct(true) 39 records without setDistinct 39 records)

final String[] models = {""};
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
ArrayList properties = new ArrayList( Arrays.asList(“Make”,“Model”));
queryBuilder.setProperties(properties);
queryBuilder.setDistinct(true);
queryBuilder.setPageSize(100);
Backendless.Data.of(“Automotive”).find(queryBuilder, new AsyncCallback<List>() {
@Override
public void handleResponse(List response) {

            for (int i = 0; i < response.size(); i++) {
                models[0] += response.get(i).get("Make")+" "+response.get(i).get("Model")+"#";
                
                Log.d(getClass().getName(), "Response Size:" + response.size());

            }
        }

        @Override
        public void handleFault(BackendlessFault fault) {

        }
    });

when you set distinct to true, the identity of every record is determined by its objectId. Therefore when you see something like this in the response:
image

this is because they are two separate distinct objects:

If you need to get unique combinations of makes and models, you should add groupBy with "Make" and "Model" as the parameters.

Regards,
Mark

Solved. Thanks.