Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Query and Full Text Search with Couchbase Mobile – Connect Silicon Valley 2017

176 views

Published on

Speaker: Priya Rajagopal

N1QL and Full Text Search (FTS) come to Couchbase Mobile! In this session you’ll be one of the first to get your hands on N1QL queries and FTS in Couchbase Mobile 2.0.

You’re building a scalable mobile app that needs fast, secure access to your embedded database. N1QL from Couchbase now combines the querying power of SQL with the modeling flexibility of JSON for your gaming, enterprise, or IoT mobile application.
Or maybe you’re building a mobile app that accesses lots of unstructured textual data coming from social media feeds or product information from an eCommerce catalog. You know that magnifying glass you see on every website? It's often full-text search. You want to be able to run faceted search or show a search result in context in your mobile app. What you need is high-performance full-text searching capabilities in your embedded database.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Query and Full Text Search with Couchbase Mobile – Connect Silicon Valley 2017

  1. 1. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. QUERY AND FULL TEXT SEARCH WITH COUCHBASE MOBILE Priya Rajagopal, Developer Advocate, Couchbase
  2. 2. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. COUCHBASE MOBILE 2.0
  3. 3. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 3 Couchbase Mobile 2.0 - Highlights • Developer Preview • New C/C++ Lite Core Engine • Replication Protocol 2.0 – “BLIP” • Binary Encoding of JSON Documents - “Fleece” • API redesigned from the ground up • N1QL-like Query & Full Text Search Interface Mobile App Couchbase Lite 2.0 Native API Native Platform Bindings JSON Lite Core Engine Fleece SQLite Engine BLIP
  4. 4. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. QUERY AND FTS WALKTHROUGH
  5. 5. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 5 Query : All Documents let searchQuery = Query .select(SelectResult.all()) .from(DataSource.database(db)) var matches:[Data] = [Data]() do { for row in try searchQuery.run() { matches.append(row.toDictionary()) } }
  6. 6. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 6 Query : Pagination let searchQuery = Query .select(SelectResult.all()) .from(DataSource.database(db)) .limit(limit, offset: offset) var matches:[Data] = [Data]() do { for row in try searchQuery.run() { matches.append(row.toDictionary()) } }
  7. 7. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 7 SelectResult • “Represents a single return value of the query statement” • SelectResult.all() : Returns all properties • SelectResult(Expression) : Return values based on Expression
  8. 8. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 8 Query Expression • Metadata • Pattern Matching • Comparison • Collection • Function • Parameter • Quantified • Bitwise Operator • Property • Aggregate • String • Collation • Regex • FTS • Null Check Operator • Arithmetic Expressions “Expression for constructing a Query Statement”
  9. 9. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 9 Query : All Documents w/ the “where” clause let searchQuery = Query .select(SelectResult.all()) .from(DataSource.database(db)) .where(Expression.property("type").equalTo(type)) .limit(limit, offset: offset) var matches:[Data] = [Data]() do { for row in try searchQuery.run() { matches.append(row.toDictionary()) } } Property Expression
  10. 10. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 10 Query : Metadata let searchQuery = Query .select(SelectResult.expression(Expression.meta().id)) .from(DataSource.database(db)) .where(Expression.property("type").equalTo(type)) .limit(limit, offset: offset) Metadata Expression
  11. 11. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 11 Query : Metadata + All Properties let searchQuery = Query . select(SelectResult.expression(Expression.meta().id), SelectResult.all()) .from(DataSource.database(db)) .where(Expression.property("type").equalTo(type)) .limit(limit, offset: offset) Response: [ { "id": "k_10019", "db_name": { ”type": ”hotel”, ”name": ”abc1" } }, { "id": "k_11029", "db_name": { ”type":”hotel”, ”name": ”abc2" } } ]
  12. 12. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 12 Query : Return Metadata + Specific Properties let searchQuery = Query .select(SelectResult.expression(Expression.property("name")), SelectResult.expression(Expression.meta().id)) .from(DataSource.database(db)) .where(Expression.property("type").equalTo(type)) .limit(limit, offset: offset) Response: [ { "id": "k_10019", "name": ”priya", }, { "id": "k_11020", "name": ”john” } ]
  13. 13. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 13 Query : Comparison & Logical Expressions let searchQuery = Query .select(SelectResult.expression(Expression.property("title")), SelectResult.expression(Expression.property("city"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel") .and(Expression.property("country").equalTo("United States") .or(Expression.property("country").equalTo("France"))) .and(Expression.property("vacancy").equalTo(true))) .limit(limit) Comparison Expression Logical Expression
  14. 14. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 14 Query : Property Expressions w/ KeyPath let searchQuery = Query .select( SelectResult.expression(Expression.property("geo.lon")), SelectResult.expression(Expression.property("geo.lat")), SelectResult.expression(Expression.property("title"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel")) .limit(limit) { "id": "k_10019", "name": "Awesome Hotel", "type": "hotel", "geo": { ”lat”:40.741895, ”lon”:-73.989308 } }
  15. 15. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 15 Query : Ordering let searchQuery = Query .select( SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("title")), SelectResult.expression(Expression.property("city"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo(type)) .orderBy(Ordering.property("title").ascending()) .limit(limit)
  16. 16. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 16 Query : Like Expression w/ Exact Match let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("country")), SelectResult.expression(Expression.property("name"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("landmark") .and( Expression.property("name").like("engineer"))) .limit(limit) Like Expression (case insensitive) • Royal Engineer Museum
  17. 17. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 17 Query : Like Expression w/ Wildcard Match let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("country")), SelectResult.expression(Expression.property("name"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("landmark") .and( Expression.property("name").like(”%eng%r%"))) .limit(limit) • Royal Engineer Museum • Bengal Raj • Barney Greengrass Like Wildcard Expression
  18. 18. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 18 Query : Like Expression w/ Character Wildcard Match let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("country")), SelectResult.expression(Expression.property("name"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("landmark") .and( Expression.property("name").like(”%eng____r%"))) .limit(limit) • Royal Engineer Museum Like Character Wilcard Expression
  19. 19. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 19 Query : Regex Match let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("name")) ) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("landmark") .and(Expression.property("name").regex("bEng.*r.*b"))) .limit(limit) Regex Case sensitive Expression • Royal Engineer Museum
  20. 20. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 20 Query : Null Check let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("email"))) .from(DataSource.database(db)) .where(Expression.property("email").isNullOrMissing()) .limit(limit) Null Check Expression
  21. 21. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 21 Collection Function Query : Collection Functions let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("name"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel") .and(Function.arrayContains(Expression.property("public_likes"), value: ”Anne May"))) .limit(limit) { "id": "k_10019", "name": "Awesome Hotel", "type": "hotel", "public_likes": [ "john appleseed", "jill doe", "Anne may" ] }
  22. 22. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 22 Query : Collection Functions let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("name")), SelectResult.expression(Function.arrayLength(Expression.property("public_likes")))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel")) .limit(limit) { "id": "k_10019", "name": "Awesome Hotel", "type": "hotel", "public_likes": [ "john appleseed", "jill doe", "Anne may" ] }
  23. 23. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 23 Query : Collection Functions let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("name")), SelectResult.expression(Function.arrayLength(Expression.property("public_likes")))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel")) .limit(limit) Response: { "id": "k_10019", "name": "Awesome Hotel", "$1": 3 }
  24. 24. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 24 Query : Collection Functions w/ aliases let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("name")), SelectResult.expression(Function.arrayLength (Expression.property("public_likes"))).as("#Likes") .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel")) .limit(limit) Response: { "id": "k_10019", "name": "Awesome Hotel", "#Likes": 3 }
  25. 25. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 25 Query : Parameterized Range Functions let likesCount = Function.arrayLength(Expression.property("public_likes")) let lowerCount = Expression.parameter("lower") let upperCount = Expression.parameter("upper") let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("name")), SelectResult.expression(likesCount).as("#Likes") ) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel") .and(likesCount.between(lowerCount,and: upperCount))) .limit(limit) searchQuery.parameters.setInt(5, forName: "lower") searchQuery.parameters.setInt(10, forName: "upper") { "id": "k_10019", "name": "Awesome Hotel", "type": "hotel", "public_likes": [ "john appleseed", "jill doe", "Anne may" ] }
  26. 26. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 26 Query : String Functions let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("email")), SelectResult.expression(Expression.property("name"))) .from(DataSource.database(db)) .where(Expression.property("email") .and(Function.contains(Expression.property("email"), substring: "nationaltrust.org"))) .limit(limit) • Function.lower(prop); • Function.ltrim(prop); • Function.rtrim(prop); • Function.trim(prop); • Function.upper(prop); • Function.substring() String Expression
  27. 27. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 27 Query : Collation Expressions let ignoreCase = Collation.unicode() .ignoreCase(false) let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("country"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("landmark") .and(Expression.property("country").collate(ignoreCase).equalTo("united states"))) .limit(limit) String Comparison w/ Collation
  28. 28. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 28 Query : Count Function let searchQuery = Query .select(SelectResult.expression(Function.count(1)).as("NumHotels")) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel")) .limit(limit) Response: { "NumHotels": 917 } Count Function
  29. 29. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 29 Query : Count Function w/ Grouping let searchQuery = Query .select(SelectResult.expression(Function.count(0)).as("NumHotels"), SelectResult.expression(Expression.property("country"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel")) .groupBy(Expression.property("country")) .limit(limit) Response: [ { "NumHotels": 140, "country": “France” }, { "NumHotels": 416, "country": “United Kingdom” }, { "NumHotels": 361, "country": “United States” } ] GroupBy Expression
  30. 30. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 30 Query : Count Function w/ Grouping let searchQuery = Query .select(SelectResult.expression(Function.count(1)).as("NumHotels"), SelectResult.expression(Expression.property(”name"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel")) .groupBy(Expression.property("country")) .limit(limit) Response: [ { "NumHotels": 140, ”name": “Hotel Formule 1” }, { "NumHotels": 416, ”name": “Two Bears Cottage” }, { "NumHotels": 361, ”name": “Wildrose” } ] No “array_agg” equivalent as yet ….
  31. 31. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 31 Query : Satisfies Expressions let NAME = Expression.variable("reviewer") let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("public_likes"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel") .and(Expression.any("reviewer").in(Expression.property("public_likes")) .satisfies(NAME.like("%Alice%")))) .limit(limit) { "id": "k_10019", "name": "Awesome Hotel", "type": "hotel", "public_likes": [ "john appleseed", "jill doe", ”Alice may" ] }
  32. 32. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 32 JOIN Use Case { "type": ”favlist", "country" : "France", "hobby":"museum", "pet":"dog", "sport":"golf” } { "type": ”landmark", "country" : "France", ”content":”This is a great museum for art lovers", ”name":”Louvre” } { "type": ”landmark", "country" : "France", ”content":”One of the tallest structures ...", ”name":”Effil Tower” } { "type": ”landmark", "country" : ”United States", ”content":”This is a great stadium for football fanatics", ”name":”The Big House” }
  33. 33. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 33 Inner JOIN JOIN Expression Query : JOIN Expression // Data Sources let favDS = DataSource.database(db).as("favDS") let landmarkDS = DataSource.database(db).as("landmarksDS") let countryExpr = Expression.property("country").from("favDS") let landmarkExpr = Expression.property("country").from("landmarksDS") // Join Expression let joinExpr = countryExpr.equalTo(landmarkExpr) .and(Expression.property("type").from("landmarksDS").equalTo("landmark")) .and(Expression.property("type").from("favDS").equalTo(”favlist")) let join = Join.join(landmarkDS).on(joinExpr)
  34. 34. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 34 Query : JOIN Expression let hobbyExpr = Expression.property("hobby").from("favDS") let contentExpr = Expression.property("content").from("landmarksDS") // Query on JOIN with filtering let searchQuery = Query.select(SelectResult.expression(Expression.property("country").from("favDS")), SelectResult.expression(Expression.property("name").from("landmarksDS")), SelectResult.expression(Expression.property("content").from("landmarksDS"))) .from(favDS) .join(join) .where(Function.contains(contentExpr, substring: hobbyExpr)) .limit(limit)
  35. 35. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 35 Query : Full Text Search let index:Index = Index.ftsIndex().on(FTSIndexItem.expression(Expression.property("reviews"))).locale("en") try db.createIndex(index, withName: "reviews") let searchQuery = Query .select(SelectResult.expression(Expression.meta().id), SelectResult.expression(Expression.property("reviews")), SelectResult.expression(Expression.property("title"))) .from(DataSource.database(db)) .where(Expression.property("type").equalTo("hotel") .and( Expression.property("reviews").match("terrible OR bad"))) .limit(limit) Match FTS Expression
  36. 36. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 36 Query : Full Text Search • MATCH can only appear at top-level, or in a top-level AND • Case Insensitive • Stemming Support danish dutch english finnish french swedish turkish spanish german hungarian italian norwegian portuguese romanian russian
  37. 37. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. 37 Want More ? • Couchbase Mobile 2.0 Developer Preview : https://developer.couchbase.com/documentation/mobile/2.0/whatsnew.html • Download Couchbase Mobile 2.0 Developer Preview from https://www.couchbase.com/downloads • Soliciting Feedback • Join the discussion at Developer Forums : https://forums.couchbase.com • Follow me on Twitter @rajagp
  38. 38. Confidential and Proprietary. Do not distribute without Couchbase consent. © Couchbase 2017. All rights reserved. THANK YOU

×