Search code examples
ignite

How to chain SQL, Text and scan queries in Apache Ignite


We have a clustered Ignite cache in which we lan to store a huge amount of data (in excess of 100 million records). We are currently using SQL queries to search for records using indices. But we have a requirement for some free text based searches and we were planning to evaluate how Text Queries can work. The free text search will be in conjunction with some SQL constraints so that the result data set is not huge. I was hoping to find a way to use the Text Search and may be scan search on the result of a SQL search (which I think could give a lot more flexibility and power to the query framework of Ignite). Is there a way to achieve this. We use Native persistence and replicated cache in our system.


Solution

  • All query kinds - Scan, SQL and Text - are independent from each other. You can't use SQL on top of Text query result directly.

    You can try to execute local Text queries on all nodes, and then filter the results manually (not using SQL, just Java code). E.g.

        Collection<List<Cache.Entry<Object, Object>>> results = ignite.compute().broadcast(() -> {
            IgniteCache<Object, Object> cache = Ignition.localIgnite().<Object, Object>cache("foo");
            TextQuery<Object, Object> qry = new TextQuery<Object, Object>(Value.class, "str").setLocal(true);
            try (QueryCursor<Cache.Entry<Object, Object>> cursor = cache.query(qry)) {
                return StreamSupport.stream(cursor.spliterator(), false)
                    .filter(e -> needToReturnEntry(e))
                    .collect(Collectors.toList());
            }
        });
        List<Cache.Entry<Object, Object>> combinedResults = results.stream()
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
    

    needToReturnEntry(e) here needs to be implemented to do the same filtering as SQL constraints would.

    Another way is to retrieve a list of primary keys from the Text query, and then add that to the SQL query. This will work if the number of keys isn't too big.

    select * from TABLE where pKey in (<keys from Text Query>) and <other constraints>