Compound view for LIKE operator


#1

Hi,

I have defined the following view

com.couchbase.lite.View viewItemsByNrInregistrareAn = database.getView(“NrInregistrareAn”);
viewItemsByNrInregistrareAn.setMap(new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if (document.get(“type”) != null) {
String type = String.valueOf(document.get(“type”));
if (“docreg”.equals(type)) {
String NrInregistrare = String.valueOf(document.get(“NrInregistrare”));
Integer An = Integer.valueOf(document.get(“An”).toString());

                    emitter.emit(new Object[]{An, NrInregistrare}, document);
                }
            }
        }
    }, "8.0");

by means of which I try to find documents using the following query
query.setStartKey(Arrays.asList(intAn));
query.setEndKey(Arrays.asList(intAn, strNrInreg, new HashMap<String, Object>()));

My data for (as string) NrInregistrare=[123, 124, 125, 1251, 130, 131, 140, 1411, 200]
Results for NrInreg/NrInregistrare:

  • ‘125’: 123, 124, 125
  • ‘126’: 123, 124, 125, 1251
  • ‘130’: 123, 124, 125, 1251, 130

It works but not the way I expect…I want to get all the documents for which the property document.get(“NrInregistrare”) starts with NrInreg, something like WHERE NrInregistrare LIKE NrInreg+’%’

Also, for the query
query.setStartKey(Arrays.asList(dtpAn.getYear(), NrInreg));
query.setEndKey(Arrays.asList(dtpAn.getYear(), NrInreg, new HashMap<String, Object>()))
I get only the documents for which the property document.get(“NrInregistrare”) = NrInreg

thanks,
catalin


#2

I can’t figure out from reading this what exactly your data is and what you expect. Can you be more specific?

For example, is NrInregistrate an array, or a string that looks like an array? (And if it’s a string, why would you convert a JSON array to a string to query it?)


#3

I’ve found my answer here, hidden in discussions:

https:// github .com/ couchbase /couchbase-lite-android/ issues/ 105

Query query = view.createQuery();

query.setStartKey(term.toLowerCase());

query.setEndKey(String.format("%s\uefff", NrInreg)); <==> WHERE NrInregistrare LIKE NrInreg+’%’

Thanks,
Catalin