Datastoreで検索エンジンを作る

29 Oct 2016

Daigo Ikeda

Knightso, LLC

Profile

Daigo Ikeda
@hogedigo

Knightso, LLC
http://www.knightso.co.jp/
Shizuoka, JAPAN

2

Datastore Indexおさらい

3

Sample: Book Store

Model:

type Book struct {
    ID          string
    Title       string
    Category    string
    Price       int
    PublishDate string
}
4

Sample: Book Store

Run Query:

q := datastore.NewQuery("Book").Limit(20)

// apply filters here

var books []*Book
_, err := q.GetAll(ctx, &books)

Equality Filter:

q = q.Filter("Category = ", "趣味")

Inequality Filter:

q = q.Filter("Price >= ", 1000)

Sort:

q = q.Order("Price")
5

Single Property Index

type Hoge struct {
    Value string `datastore:",noindex"`
}
6

Composite Property Index

index.yaml

indexes:

- kind: Book
  properties:
  - name: Category
  - name: Price
    direction: desc
7

Index Example

8

List Property

type Hoge struct {
    Values []string
}
9

List Property Index Example

10

ZigZag Merge JOIN

11

Datastoreだけで検索エンジン作ろう!

12

How

N-gram(Bigram)

AppEngine → ap, pp, pe, en, ng, gi, in, ne

Prefix

AppEngine → a, ap, app, appe, appen, appeng, appengi, appengin, appengine

形態素解析

貴社の記者が汽車で帰社した → 貴社, の, 記者, が, 汽車, で, 帰社, した

併用

13

Why not Search API?

// search for documents with pianos that cost less than $5000
index.Search(ctx, "Product = piano AND Price < 5000", nil)
14

Why not BigQuery?

15

Why not Cloud SQL?

16

実装

17

Entity Model

type Book struct {
    ID          string `datastore:",noindex"`
    Title       string `datastore:",noindex"`
    Category    string `datastore:",noindex"`
    Price       int    `datastore:",noindex"`
    PublishDate string `datastore:",noindex"`
}
18

Index Model

インデックス保存用のエンティティを用意する

type BookIndex struct {
    Indexes     []string
    Title       string
    Category    string
    Price       int
    PublishDate string
}

Bookと同じKey名で保存する(Kindが異なる)

Indexesプロパティに解析したインデックスを保存
ソートに必要なプロパティも保存

19

Save Indexes

20

Composite Property Index

eg) ORDER BY PublishDate DESC, Price, Category

index.yaml

- kind: BookIndex
  properties:
  - name: Indexes
  - name: PublishDate
    direction: desc
  - name: Price
  - name: Category
21

Search!

q := datastore.NewQuery(KindBookIndex).Limit(QUERY_LIMIT + 1).KeysOnly()

if req.Title != "" {
    for _, w := range bigram(req.Title) {
        q = q.Filter("Indexes =", "t " + w)
    }
}

if req.Category != "" {
    q = q.Filter("Indexes =", "c " + req.Category)
}

if req.Price != "" {
    q = q.Filter("Indexes =", "p " + req.Price)
}

// 最初のソートに指定したプロパティにはInequality Filterを使える! 
if req.PublishDateFrom != "" {
    q = q.Filter("PublishDate >=", req.PublishDateFrom)
}
22

if noParams {
    q = q.Filter("Indexes =", createIndex("", "ALL"))
}

q = q.Order("-PublishDate").Order("Price").Order("Category")

keys := make([]*datastore.Key, 0, QUERY_LIMIT)

ite := q.Run(ctx)

for len(keys) < QUERY_LIMIT {
    idxKey, err := ite.Next(nil)
    if err == datastore.Done {
        break
    }

    ...snip...

    key := datastore.NewKey(ctx, KindBook, idxKey.StringID(), 0, nil)
    keys = append(keys, key)
}
23

books := make([]*Book, len(keys))
if len(books) > 0 {
    if err := datastore.GetMulti(ctx, keys, books); err != nil {
        return nil, fmt.Errorf("GetMulti failed: %s", err)
    }
}
24

On-Memory Filter

Inequality Filterは最初のソート対象プロパティに対してしか適用できない
他プロパティに対してフィルターかけたい場合はプログラムでやるしかない

//KeysOnly外す
for len(keys) < QUERY_LIMIT {
    var book Book
    idxKey, err := ite.Next(&book)

    ...snip...

    if book.Price < priceFrom || book.Price >= priceTo {
        continue
    }

    books = append(books, book)
}
25

独自複合インデックス

Merge Joinにも弱点がある

上記を解決する為に、よく使用される条件の組み合わせで予めインデックスを作成しておく。

26

Projection Query

SELECT PublishDate, Price, Category FROM Book

必要なプロパティのみ取得

q := datastore.NewQuery(KindBookIndex).
    Project("PublishDate", "Price", "Category").
    Limit(QUERY_LIMIT + 1)
27

まとめ

28

Pros

29

Cons

30

Single Propety Indexだけで実装したい!

31

Index Model

type BookIndex struct {
    Indexes []string
}
32

Index Key

eg) ORDER BY Price, PublishDate DESC, Category

<Price>:<PublishDate>:<Category>:<ID>
33

34

Thank you

29 Oct 2016

Daigo Ikeda

Knightso, LLC

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)