title: Cassandra Full-Text Search
body: <p>Full-Text search in Cassandra;</p>
  
  <p>I am fairly new to Cassandra, and wish to understand it more properly. I am attempting to perform a Full-Text search in Cassandra, but after some research I have found that there <em>may</em> not be a "simple" approach for this.. and I say maybe because the first page of Google hasn't said much of anything. </p>
  
  <p>So I am trying to understand now instead, what is the best approach here.. This sort of lead me to take make up my own assumptions based on what I've learned so far about Cassandra, that is based on these two principals; a) design your tables based on your queries, rather than the data, and b) more-data is a good thing, as long as it is being used properly.</p>
  
  <p>With that being said, I've come up with a couple of solutions I'd like to share, and also ask that if anyone has a better idea, please fill me on it before I commit to anything unreasonable/naive. </p>
  
  <p>First Solution: Create a Column Family(CF), with two primary keys and an Index like so:</p>
  
  <pre><code>CREATE TABLE "FullTextSearch" (
  "PartialText" text,
  "TargetIdentifier" uuid,
  "CompleteText" text,
  "Type" int,
  PRIMARY KEY ("PartialText","TargetIdentifier")
  );
  CREATE INDEX IX_FullTextSearch_Type "keyspace"."FullTextSearch" ("Type");
  </code></pre>
  
  <p>With the above table, I would need to insert rows for the text "Hello World" as follows:</p>
  
  <pre><code>BATCH APPLY;
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("H",000000000-0000-0000-0000-000000000,"Hello World",1);
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("He",000000000-0000-0000-0000-000000000,"Hello World",1);
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("Hel",000000000-0000-0000-0000-000000000,"Hello World",1);
  .....
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("Hello Wor",000000000-0000-0000-0000-000000000,"Hello World",1);
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("Hello Worl",000000000-0000-0000-0000-000000000,"Hello World",1);
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("Hello World",000000000-0000-0000-0000-000000000,"Hello World",1);
  .....
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("Wor",000000000-0000-0000-0000-000000000,"Hello World",1);
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("Worl",000000000-0000-0000-0000-000000000,"Hello World",1);
  INSERT INTO "FullTextSearch" ("PartialText","TargetIdentifier","CompleteText","Type") VALUES ("World",000000000-0000-0000-0000-000000000,"Hello World",1);
  END BATCH;
  </code></pre>
  
  <p>Basically, the above will satisfy the following wildcards/partialtext "%o W%", "Hello%", "Worl%"; However it will not satisfy partial words such as "%ell%" for "Hello", which I can feel <em>alright</em> about for now..... (OCD sorta kicks in here)</p>
  
  <p>This approach sort of sucks for me because I would now have to delete/re-insert any time a save/name change occurs on the "TargetIdentifier";</p>
  
  <p>The Second Solution, would be very similar only this time making use of wide-columns; where the table might look like:</p>
  
  <pre><code>CREATE TABLE "FullTextSearch" (
  "TargetIdentifier" uuid,
  "Type" int,
  "CompleteText" text,
  PRIMARY KEY("TargetIdentifier")
  );
  </code></pre>
  
  <p>and now during a search something like:</p>
  
  <pre><code>SELECT * FROM "FullTextSearch" WHERE "He" = 1;
  </code></pre>
  
  <p>so that if the column exists, the respective rows are returned;</p>
  
  <p>Third Solution:
  similar to the one above, only this time instead of using wide-columns we use a set column such as map for the partial texts, and perform a query like:</p>
  
  <pre><code>SELECT * FROM "FullTextSearch" WHERE "PartialTexts"['He'] = 1;
  </code></pre>
  
  <p>Anyways, I am all out of ideas, it is late, and I can only hope for a great response! Please, let me know what I should be doing here... am I even on the right path?</p>