39
Document relations Martijn van Groningen @mvgroningen Tuesday, November 6, 12

Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Document relationsMartijn van Groningen

@mvgroningen

Tuesday, November 6, 12

Page 2: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Background

• Document relations with joining.

• Various solutions in Lucene and Elasticsearch

Overview

Tuesday, November 6, 12

Page 3: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Lucene is document based.

• Lucene doesn’t store information about relations between documents.

• Data often holds relations.

• Good free text search over relational data.

Background - Lucene model

Tuesday, November 6, 12

Page 4: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Compound documents.

• May result in documents with many fields.

• Subsequent searches.

• May cause a lot of network overhead.

• Non Lucene based approach:

• Use Lucene in combination with a relational database.

Background - Common solutions

Tuesday, November 6, 12

Page 5: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Product

• Name

• Description

• Product-item

• Color

• Size

• Price

Background - Example

Tuesday, November 6, 12

Page 6: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Compound Product & Product-items document.

• Each product-item has its own field prefix.

Background - Example

Tuesday, November 6, 12

Page 7: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Lucene offers solutions to have a 'relational' like search.

• Joining

• Result grouping

• Elasticsearch builds on top of the joining capabilities.

• These solutions aren't naturally supported.

Background - Other solutions

Tuesday, November 6, 12

Page 8: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Joining

Tuesday, November 6, 12

Page 9: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Join support available since Lucene 3.4

• Not a SQL join!

• Two distinct joining types:

• Index time join

• Query time join

• Joining provides a solution to handle document relations.

Joining

Tuesday, November 6, 12

Page 10: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Joining - What is out there?

• Index time:

• Lucene’s block join implementation.

• Elasticsearch’s nested filter, query and facets.

• Built on top of the Lucene’s block join support.

• Query time:

• Lucene’s query time join utility.

• Solr’s join query parser.

• Elasticsearch’s various parent-child queries and filters.

Tuesday, November 6, 12

Page 11: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Index time joinAnd nested documents.

Tuesday, November 6, 12

Page 12: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Lucene block join queries:

• ToParentBlockJoinQuery

• ToChildBlockJoinQuery

• Lucene collector:

• ToParentBlockJoinCollector

• Index time join requires block indexing.

Joining - Block join query

Tuesday, November 6, 12

Page 13: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Atomically adding documents.

• A block of documents.

• Each document gets sequentially assigned Lucene document id.

• IndexWriter#addDocuments(docs);

Joining - Block indexing

Tuesday, November 6, 12

Page 14: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Index doesn't record blocks.

• App is responsible for identifying block documents.

• Segment merging doesn’t re-order documents in a segment.

• Adding a document to a block requires you to reindex the whole block.

Joining - Block indexing

Tuesday, November 6, 12

Page 15: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Parent is the last document in a block.

Joining - block join query

Tuesday, November 6, 12

Page 16: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Marking parent documents

Block join - ToChildBlockJoinQuery

Tuesday, November 6, 12

Page 17: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Add block

Add block

Block join - ToChildBlockJoinQuery

Tuesday, November 6, 12

Page 18: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Parent filter marks the parent documents.

• Child query is executed in the parent space.

Block join - ToChildBlockJoinQuery

Tuesday, November 6, 12

Page 19: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Block join & Elasticsearch

• In Elasticsearch exposed as nested objects.

• Documents are constructed as JSON.

• JSON’s nested structure works nicely with block indexing.

• Elasticsearch takes care of block indexing and also keeps track of the nested documents.

Tuesday, November 6, 12

Page 20: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Elasticsearch’s nested support

• Support for a nested type in mappings.

• Nested query.

• Nested filter.

• Nested facets.

Tuesday, November 6, 12

Page 21: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Nested type

• The nested types enables Lucene’s block indexing.

curl -XPUT 'localhost:9200/products' -d '{"mappings" : {

"product" : {"properties" : {

"offers" : { "type" : "nested" }}

}}

}'

index

type

Nested offers

Tuesday, November 6, 12

Page 22: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Indexing nested objects

curl -XPOST 'localhost:9200/products/product' -d '{"name" : "Polo shirt","description" : "Made of 100% cotton","offers" : [

{"color" : "red","size" : "s","price" : 999

},{

"color" : "red","size" : "m","price" : 1099

},{

"color" : "blue","size" : "s","price" : 999

}]

}'

index type

nested objects

Tuesday, November 6, 12

Page 23: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Nested query

curl -XPOST 'localhost:9200/products/product/_search' -d '{"query" : {

"nested" : { "path" : "offers", "score_mode" : "total", "query" : { "bool" : { "must" : [ { "term" : { "color" : "blue" } }, { "term" : { "size" : "m" } } ] } }}

}}'

Color red would match the previous document.

The nested field path in mapping.

Sum the individual nested matches.

Tuesday, November 6, 12

Page 24: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Nested facetscurl -XPOST 'localhost:9200/products/product/_search' -d '{

"facets" : {"color" : {

"terms_stats" : {"key_field" : "size","value_field" : "price"

},"nested" : "offers"

}}

}'

Counts 2 nested documentsfor term: s

"facets":{ "color":{ "_type":"terms_stats", "missing":0, "terms":[ { "term":"s", "count":2, "total_count":2, "min":999.0, "max":999.0, "total":1998.0, "mean":999.0 }, ... ] }}

A facet for nested field offers.

Tuesday, November 6, 12

Page 25: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Query time joinand parent & child relations.

Tuesday, November 6, 12

Page 26: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Query time joining

• Documents are joined during query time.

• More expensive, but more flexible.

• Two types of query time joins:

• Parent child joining.

• Field based joining.

Tuesday, November 6, 12

Page 27: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Query time joining is executed in two phases.

• Field based joining:

• ‘from’ field

• ‘to’ field

• Doesn’t require block indexing.

Lucene’s query time join

Tuesday, November 6, 12

Page 28: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• First phase collects all the terms in the fromField for the documents that match with the original query.

• The second phase returns the documents that match with the collected terms from the previous phase in the toField.

• One public method:

• JoinUtil#createJoinQuery(...)

Query time join - JoinUtil

Tuesday, November 6, 12

Page 29: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Referrer the product id.

Joining - JoinUtil

Tuesday, November 6, 12

Page 30: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Joining - JoinUtil

Tuesday, November 6, 12

Page 31: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Result will contain one products.

• Possible to do ‘join’ across indices.

Joining - JoinUtil

Join utility

Tuesday, November 6, 12

Page 32: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Elasticsearch’s query time join

• A parent child solution.

• Not related to Lucene’s query time join.

• Support consists out of:

• The _parent field.

• The top_children query.

• The has_parent & has_child filter & query.

• Scoped facets.

Tuesday, November 6, 12

Page 33: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

The _parent field

• Points to the parent type.

• Mapping attribute to be define on the child type.

• Elasticsearch uses the _parent field to build an id cache.

• Makes parent/child queries & filters fast.

curl -XPUT 'localhost:9200/products' -d '{"mappings" : {

"offer" : {"_parent" : {

"type" : "product"}

}}

}'

Tuesday, November 6, 12

Page 34: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

curl -XPOST 'localhost:9200/products/offer?parent=1' -d '{"color" : "blue","size" : "s","price" : 999

}'

Indexing parent & child documents

• Parent document:

• Child documents:

curl -XPOST 'localhost:9200/products/product/1' -d '{"name" : "Polo shirt","description" : "Made of 100% cotton"

}'

curl -XPOST 'localhost:9200/products/offer?parent=1' -d '{"color" : "red","size" : "s","price" : 999

}'

curl -XPOST 'localhost:9200/products/offer?parent=1' -d '{"color" : "red","size" : "m","price" : 1099

}'

The id of the parent document. Also used for

routing.

Tuesday, November 6, 12

Page 35: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

The ‘top_children’ query

• Internally the child query is potentially executed several times in order to get enough parent hits.

curl -XPOST 'localhost:9200/products/_search' -d '{"query" : {

"top_children" : {"type" : "offer","query" : {

"term" : {"size" : "m"

}},"score" : "sum"

}}

}'

Child type

Score mode

Child query

Tuesday, November 6, 12

Page 36: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

The ‘has_child’ query

• Doesn’t map the child scores into the matching parent doc. Works as a filter.

• The has_parent query matches child document instead.

curl -XPOST 'localhost:9200/products/_search' -d '{"query" : {

"has_child" : {"type" : "offer","query" : {

"term" : {"size" : "m"

}}

}}

}'

Child type

Child query

Tuesday, November 6, 12

Page 37: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Scoped facets

curl -XPOST 'localhost:9200/products/_search' -d '{"query" : {

"has_child" : {"type" : "offer","query" : {

"term" : {"size" : "m"

}},"_scope" : "my_scope"

}},"facets" : {

"color" : {"terms_stats" : {

"key_field" : "size","value_field" : "price"

},"scope" : "my_scope"

}}

}'

Execute facets inside a specific scope.

Tuesday, November 6, 12

Page 38: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

• Block join & nested object are fast and efficient, but lack flexibility.

• Query time and parent child join are flexible at the cost of performance and memory.

• Field based query time joining is the most flexible.

• Parent child based joining is the fastest.

• Faceting in combination with document relations gives a nice analytical view.

Conclusion

Tuesday, November 6, 12

Page 39: Document relations - ApacheConarchive.apachecon.com/eu2012/presentations/06-Tuesday/PR-Lucen… · •Lucene is document based. • Lucene doesn’t store information about relations

Any questions?

Tuesday, November 6, 12