DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now
  1. DZone
  2. Data Engineering
  3. Data
  4. Secondary Indexes or Full-Text Search?
Content provided by Couchbase logo

Secondary Indexes or Full-Text Search?

Don Pinto user avatar by
Don Pinto
·
Mar. 23, 13 · Interview
Like (0)
Save
Tweet
Share
3.60K Views

Applications get data from Couchbase Server in different ways - they can use basic key-value operations, secondary indexes (views) or full-text search. As a developer, how do you decide whether you should use secondary indexes or full-text search for your new app feature? This blog explains the differences between secondary indexes and full-text search indexes so that you know what you should use to access data in Couchbase based on the scenario you have at hand.

Views in couchbase server are defined in javascript using a map function, which pulls out data from your documents and an optional reduce function that aggregates the data emitted by the map function. In the map function, you can specify what attributes to build the index on. Views are eventually indexed and queries are eventually consistent with respect to the documents stored. 

Visually, this is how a data structure for a secondary index looks like - 

Using a B-tree data structure for secondary indexes optimizes quick key based lookups (in this case ‘Item name’) and range queries. For example, imagine that you are building a product catalog app and want to list all the product names that starting with ‘A’ till ‘F’. Using a secondary index in Couchbase on ‘item name’, only parts of the B-tree data nodes would  need to be accessed.  

So why use full-text search?

Imagine that you want to list all the products in your store having the keyword ‘red’ - this includes items such ‘red sweaters’, ‘red pants’ or even items with the color attribute ‘red’. A full-text search index maps document terms to the the list of document id’s - which means you can quickly get back the list of document id’s that have a particular term in them. 

Couchbase server integrates with Elasticsearch, a full-text search engine. Using the Couchbase adapter for Elasticsearch, documents are replicated in real-time to Elasticsearch. Elasticsearch parses each document and builds a full-text index so that you can search across all your documents from your app.

The figure above shows how a full-text search index maps document terms found in the documents to document IDs. This data-structure is elegant for ad-hoc search querying - so for example, if you’re looking for “sweaters”, you get the document id’s relevant to  Red and Blue sweaters.

Now that you understand about secondary indexes and full-text search indexes, let’s take a look at when you should use full-text search and when you should consider using a secondary index in your app. 

You should use full-text search when :

-  you want to search through large amounts of textual data such as web page content, blog posts, digital articles and, content metadata. Full-text search indexes will allow you to search across the entire dataset, across any attribute in addition to some relevance form of ranking the results.

-  your app needs term based search.

You should use secondary search when :

 -  you have queries in your app that run over and over again.
 
 -  you know exactly which attributes to query on based on your application. Your queries have exact matches or range queries. For example, you want to get item number “1000” or want a list of all the documents of type “pants” and sizes between 5 to 10. 
 
So, when you’re building your next app feature on Couchbase and deciding whether to use a secondary index or a full-text search index, try to apply some of the guidelines above when selecting the best index to use for your specific use-case. If you're interested to learn more about Couchbase Server and Elasticsearch, register now and don't miss the the upcoming webinar.
 

Comments

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: