-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to limit results retrieved from Lucene
Allows the records (rids) retrieved from the Lucene search to be limited, where it is known that the remainder of the query does not require the entire set to be loaded. This is useful when the underlying Lucene query returns many results, but the query overall is only intended to return a small number of them (usually in the ranked order from Lucene). This mode is opt in, by providing a "limit" metadata element to the Lucene search function. A value of "select' uses the skip/limit in the SELECT statement to determine the max hits, and an integral value specifies an explicit max hits (e.g. for a safety margin).
- Loading branch information
Showing
8 changed files
with
146 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
lucene/src/test/java/com/orientechnologies/lucene/tests/OLuceneLimitResultsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* | ||
* * Copyright 2010-2016 OrientDB LTD (http://orientdb.com) | ||
* * | ||
* * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* * you may not use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * http://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.orientechnologies.lucene.tests; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.orientechnologies.orient.core.sql.executor.OResult; | ||
import com.orientechnologies.orient.core.sql.executor.OResultSet; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class OLuceneLimitResultsTest extends OLuceneBaseTest { | ||
|
||
@Before | ||
public void init() { | ||
InputStream stream = ClassLoader.getSystemResourceAsStream("testLuceneIndex.sql"); | ||
|
||
db.execute("sql", getScriptFromStream(stream)); | ||
|
||
db.command("create index Song.title on Song (title) FULLTEXT ENGINE LUCENE"); | ||
} | ||
|
||
private void checkSongTitleHits( | ||
String query, int expectedResultSetSize, int expectedTotalHits, int expectedReturnedHits) { | ||
OResultSet docs = db.query(query); | ||
|
||
List<OResult> results = docs.stream().collect(Collectors.toList()); | ||
assertThat(results).hasSize(expectedResultSetSize); | ||
|
||
OResult doc = results.get(0); | ||
System.out.println("doc.toElement().toJSON() = " + doc.toElement().toJSON()); | ||
|
||
assertThat(doc.<Long>getProperty("$totalHits")).isEqualTo(expectedTotalHits); | ||
assertThat(doc.<Long>getProperty("$Song_title_totalHits")).isEqualTo(expectedTotalHits); | ||
assertThat(doc.<Long>getProperty("$returnedHits")).isEqualTo(expectedReturnedHits); | ||
assertThat(doc.<Long>getProperty("$Song_title_returnedHits")).isEqualTo(expectedReturnedHits); | ||
docs.close(); | ||
} | ||
|
||
@Test | ||
public void testLimitSelect() { | ||
checkSongTitleHits( | ||
"select *,$totalHits,$Song_title_totalHits,$returnedHits,$Song_title_returnedHits " | ||
+ "from Song where search_class('title:man', {\"limit\":\"select\"})= true limit 1", | ||
1, | ||
14, | ||
1); | ||
|
||
checkSongTitleHits( | ||
"select *,$totalHits,$Song_title_totalHits,$returnedHits,$Song_title_returnedHits " | ||
+ "from Song where search_class('title:man', {\"limit\":\"select\"})= true skip 5 limit 5", | ||
5, | ||
14, | ||
10); | ||
} | ||
|
||
@Test | ||
public void testLimitByNumber() { | ||
checkSongTitleHits( | ||
"select *,$totalHits,$Song_title_totalHits,$returnedHits,$Song_title_returnedHits from Song " | ||
+ "where search_class('title:man', {\"limit\": 5})= true limit 1", | ||
1, | ||
14, | ||
5); | ||
|
||
checkSongTitleHits( | ||
"select *,$totalHits,$Song_title_totalHits,$returnedHits,$Song_title_returnedHits from Song " | ||
+ "where search_class('title:man', {\"limit\": 5})= true limit 10", | ||
5, | ||
14, | ||
5); | ||
} | ||
} |