JOINS in Lucene

2019-03-28 06:27发布

Is there any way to implement JOINS in Lucene?

标签: join lucene
8条回答
劫难
3楼-- · 2019-03-28 07:12

A little late but you could use Package org.apache.lucene.search.join : https://lucene.apache.org/core/6_3_0/join/org/apache/lucene/search/join/package-summary.html

From their documentation:

The index-time joining support joins while searching, where joined documents are indexed as a single document block using IndexWriter.addDocuments().

   String fromField = "from"; // Name of the from field
   boolean multipleValuesPerDocument = false; // Set only yo true in the case when your fromField has multiple values per document in your index
   String toField = "to"; // Name of the to field
   ScoreMode scoreMode = ScoreMode.Max // Defines how the scores are translated into the other side of the join.
   Query fromQuery = new TermQuery(new Term("content", searchTerm)); // Query executed to collect from values to join to the to values

   Query joinQuery = JoinUtil.createJoinQuery(fromField, multipleValuesPerDocument, toField, fromQuery, fromSearcher, scoreMode);
   TopDocs topDocs = toSearcher.search(joinQuery, 10); // Note: toSearcher can be the same as the fromSearcher
   // Render topDocs...
查看更多
你好瞎i
4楼-- · 2019-03-28 07:17

There are some implementations on the top of Lucene that make those kind of joins among several different indexes possible. Numere (http://numere.stela.org.br/) enable that and make it possible to get results as a RDBMS result set.

查看更多
forever°为你锁心
5楼-- · 2019-03-28 07:20

You can also use the new BlockJoinQuery; I described it in a blog post here:

http://blog.mikemccandless.com/2012/01/searching-relational-content-with.html

查看更多
做个烂人
6楼-- · 2019-03-28 07:25

Lucene does not support relationships between documents, but a join is nothing else but a specific combination of multiple AND within parenthesis, but you will need to flatten the relationship first.

Sample (SQL => Lucene):

SQL:

SELECT Order.* FROM Order
JOIN Customer ON Order.CustomerID = Customer.ID
WHERE Customer.Name = 'SomeName'
AND Order.Nr = 400

Lucene:
Make sure you have all the neccessary fields and their respective values on the document like: Customer.Name => "Customer_Name" and
Order.Nr => "Order_Nr"

The query would then be:

( Customer_Name:"SomeName" AND Order_Nr:"400" )
查看更多
登录 后发表回答