Proposal for nested document support in Lucene


Published on

Published in: Technology

Proposal for nested document support in Lucene

  1. 1. Nested Documents in Lucene<br />High-performance support for parent/child document relations<br /><br />
  2. 2. Problem:<br />The Lucene data model is based on Documents, Fields and Terms. However many real-world data structures cannot be properly represented when collapsed into a single Lucene document.<br />Single<br />Lucene<br />document<br />
  3. 3. Problem: “Cross-matching”<br />When two or more data structures of the same type are jumbled up into a single Lucene field, matching logic becomes confused e.g. >1 qualification in a resume<br />John<br />Name<br />John<br />A1 in Maths<br />A1, E1<br />Grade<br />E1 in Science<br />Subject<br />Maths, Science<br />!<br />False match for query:<br />Grade:A1 AND Subject:Science<br />
  4. 4. Unacceptable solution #1<br />One modeling approach is to store related items in the same field and use proximity operators in queries<br />Name<br />John<br />A1 Maths….E1 Science<br />GradeAndSubject<br />John<br />Example query:<br /> “GradeAndSubject:”A1 Science”~2<br />A1 in Maths<br />E1 in Science<br />!<br />Slow<br />!<br />Not scalable with number of fields <br /><ul><li>Loss of fieldnames as context in query
  5. 5. Proximity distances must grow.
  6. 6. Only one choice of Analyzer for given field </li></li></ul><li>Unacceptable solution #2<br />Use numbered fieldnames to group related structures<br />Name<br />John<br />Example query:<br />( Grade1:A1 AND<br /> Subject1:Science) <br />OR <br />(Grade2:A1 AND <br />Subject2:Science )<br />…<br />A1<br />Grade1<br />Maths<br />Subject1<br />E1<br />Grade2<br />John<br />Subject2<br />Science<br />A1 in Maths<br />E1 in Science<br />!<br />Slow<br />!<br />Not scalable with number of nested structures<br /><ul><li>More numbered fieldnames = more query complexity and more unique tokens in index</li></li></ul><li>Solution: Nested documents<br />The existing Lucene codebase can be used to simply store multiple “nested” documents to represent arbitrarily complex structures. Related documents are just added in sequence<br />John<br />Name<br />John<br />A1 in Maths<br />A1<br />E1<br />Grade<br />Grade<br />E1 in Science<br />Subject<br />Maths<br />Subject<br />Science<br />?<br />But how to query?....<br />
  7. 7. Solution: Nested Document Queries<br />Nested documents need to be queried using new NestedDocumentQuery class which understands document relationships<br />John<br />Name<br />A1<br />E1<br />Grade<br />Grade<br />docType<br />resume<br />Subject<br />Maths<br />Subject<br />Science<br />New NestedDocumentQuery<br /><ul><li> Executes child search using any arbitrary Lucene Query object e.g. Boolean, fuzzy, numeric etc
  8. 8. Reports any matches as a match on the parent document not the child
  9. 9. Super-fast evaluation of joins between child and parent
  10. 10. Requires an indexed field to identify parent documents</li></ul>?<br />
  11. 11. Solution: Example Query<br />Find resume of person called “John” with A1 grade in Maths<br />John<br />Name<br />E1<br />A1<br />resume<br />Grade<br />docType<br />Grade<br />Subject<br />Science<br />Subject<br />Maths<br />The NestedDocumentQuery wrapper simply translates the stream of reported matches from the child-level query criteria into matches on the parent for evaluation of all the parent-level logic<br />
  12. 12. Solution: Join speed<br />Unlike a database, the cost of a join (child to parent) is blisteringly fast<br />3) Find first prior set bit e.g. position #356,670<br />100000100000000100000001000000010000001000010000000001000000100000100001<br />2) Index directly into cached BitSet at position #356,675<br />1) Match reported on document #356,675<br />ParentQuery<br />4) Attribute match to doc #356,670<br />NestedDocumentQuery<br />ChildQuery<br />The BitSet for defining parents is obtained from a Filter and can be cached aggressively with minimal memory cost (one bit per document in the index)<br />
  13. 13. Other advantages<br />Parent-child document relationships can also be used to limit child results from any one parent (e.g. efficiently control the max number of pages returned from any one website)<br />Nesting levels can be arbitrarily deep <br />Very powerful multi-child queries possible e.g. find people likely to know person X using resume’s employment histories (multiple employer names/urls and related date-ranges)<br />
  14. 14. “Lucene is not a database”, but…..<br />Structure matters<br />Many data sources are a mix of structured and unstructured content (e.g. microformats). This is unlikely to change. Lucene has historically been about unstructured text but has steadily been adding structured capability (Trie, spatial, facets) and become a great solution for hybrid data. However support for modeling and querying non-trivial data structures is missing currently.<br />Relationships matter<br />This proposal is not to recreate the full capabilities of a SQL database with arbitrary relationships. However we can benefit greatly from providing simple parent-child relationships<br />We have some unique capabilities<br />Parent-child joins are very fast<br />Unlike SQL we can return partial, relevance-ranked matches<br />Probably more akin to XML databases than SQL databases<br />
  15. 15. Next steps<br />Existing code/unit tests can be released to Lucene project if there is sufficient interest. This software has been deployed in production on large datasets.<br />The matching approach is reliant on parents and children being held in the same Lucene index segment. Additional control is needed to enforce this more rigorously - either by <br />Adding more user-control over IndexWritersegment creation where applications understand/control parent-child dependencies OR<br />Making Lucene aware of parent-child relationships e.g. new method Document.add(Document) <br />Query parser support<br />XML Query Parser support is available<br />End-user Query parser could add new syntax e.g. +candidateLocale:UK +child(grade:A1 AND subject:music)<br />
  16. 16. Thoughts?<br />Feedback encouraged on<br />