lucene query time join (关联搜索)

  categories:搜索资料  tags:,   author:

query  time join  已经在solr存在一段时间了但是lucene中有这个终归是好事,多一种选择嘛,实现这种关联的document还是很实用的,可以实现部分关联查询;

更新属性的时候也可以设计好结构,更新部分索引了。

这个解释有点费劲,还是看代码吧,

  1. final String idField = “id”;
  2. +    final String toField = “productId”;
  3. +
  4. +    Directory dir = newDirectory();
  5. +    RandomIndexWriter w = new RandomIndexWriter(
  6. +        random,
  7. +        dir,
  8. +        newIndexWriterConfig(TEST_VERSION_CURRENT,
  9. +            new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
  10. +
  11. +    // 0
  12. +    Document doc = new Document();
  13. +    doc.add(new Field(“description”, “random text”, TextField.TYPE_STORED));
  14. +    doc.add(new Field(“name”, “name1″, TextField.TYPE_STORED));
  15. +    doc.add(new Field(idField, “1”, TextField.TYPE_STORED));
  16. +    w.addDocument(doc);
  17. +
  18. +    // 1
  19. +    doc = new Document();
  20. +    doc.add(new Field(“price”, “10.0”, TextField.TYPE_STORED));
  21. +    doc.add(new Field(idField, “2”, TextField.TYPE_STORED));
  22. +    doc.add(new Field(toField, “1”, TextField.TYPE_STORED));
  23. +    w.addDocument(doc);
  24. +
  25. +    // 2
  26. +    doc = new Document();
  27. +    doc.add(new Field(“price”, “20.0”, TextField.TYPE_STORED));
  28. +    doc.add(new Field(idField, “3”, TextField.TYPE_STORED));
  29. +    doc.add(new Field(toField, “1”, TextField.TYPE_STORED));
  30. +    w.addDocument(doc);
  31. +
  32. +    // 3
  33. +    doc = new Document();
  34. +    doc.add(new Field(“description”, “more random text”, TextField.TYPE_STORED));
  35. +    doc.add(new Field(“name”, “name2″, TextField.TYPE_STORED));
  36. +    doc.add(new Field(idField, “4”, TextField.TYPE_STORED));
  37. +    w.addDocument(doc);
  38. +    w.commit();
  39. +
  40. +    // 4
  41. +    doc = new Document();
  42. +    doc.add(new Field(“price”, “10.0”, TextField.TYPE_STORED));
  43. +    doc.add(new Field(idField, “5”, TextField.TYPE_STORED));
  44. +    doc.add(new Field(toField, “4”, TextField.TYPE_STORED));
  45. +    w.addDocument(doc);
  46. +
  47. +    // 5
  48. +    doc = new Document();
  49. +    doc.add(new Field(“price”, “20.0”, TextField.TYPE_STORED));
  50. +    doc.add(new Field(idField, “6”, TextField.TYPE_STORED));
  51. +    doc.add(new Field(toField, “4”, TextField.TYPE_STORED));
  52. +    w.addDocument(doc);
  53. +
  54. +    IndexSearcher indexSearcher = new IndexSearcher(w.getReader());
  55. +    w.close();
  56. +
  57. +    // Search for product
  58. +    Query joinQuery =
  59. +        JoinUtil.createJoinQuery(idField, false, toField, new TermQuery(new Term(“name”, “name2″)), indexSearcher);
  60. +
  61. +    TopDocs result = indexSearcher.search(joinQuery, 10);
  62. +    assertEquals(2, result.totalHits);
  63. +    assertEquals(4, result.scoreDocs[0].doc);
  64. +    assertEquals(5, result.scoreDocs[1].doc);

上面的

JoinUtil.createJoinQuery(idField, false, toField, new TermQuery(new Term(“name”, “name2″)), indexSearcher);

是lucene4.0中的,现在还是alphe版本,

在3.6中没有第二个boolean参数,据说4.0的要比3.6的 query  time join快3倍,没验证过。



快乐成长 每天进步一点点