use of com.rbmhtechnology.vind.api.result.InverseSearchResult in project vind by RBMHTechnology.
the class InverseSearchTest method testInverseSearchIntegration.
@Test
@RunWithBackend(Elastic)
public void testInverseSearchIntegration() throws IOException {
final SearchServer server = testBackend.getSearchServer();
final MultiValueFieldDescriptor.TextFieldDescriptor<String> metadata = new FieldDescriptorBuilder<>().setFullText(true).setFacet(true).buildMultivaluedTextField("customMetadata");
final SingleValueFieldDescriptor.TextFieldDescriptor<String> tenant = new FieldDescriptorBuilder<>().setFullText(true).setFacet(true).buildTextField("tenant");
final DocumentFactory testDocsFactory = new DocumentFactoryBuilder("TestDocument").addField(metadata).addInverseSearchMetaField(tenant).build();
Document resource1 = testDocsFactory.createDoc("1").setValues(metadata, "meta=data", "meta2=data2");
Document resource2 = testDocsFactory.createDoc("2").setValues(metadata, "meta2=data2", "meta3=data3");
final FilterLuceneParser filterLuceneParser = new FilterLuceneParser();
Filter ruleFilter = filterLuceneParser.parse("customMetadata:(\"meta=data\" OR \"meta2=data2\")", testDocsFactory);
final InverseSearchQuery rule1 = testDocsFactory.createInverseSearchQuery("rule1", ruleFilter).setValue(tenant, "t1");
server.addInverseSearchQuery(rule1);
Filter ruleFilter2 = filterLuceneParser.parse("customMetadata:(\"meta=data\" AND \"meta2=data2\")", testDocsFactory);
final InverseSearchQuery rule2 = testDocsFactory.createInverseSearchQuery("rule2", ruleFilter2).setValue(tenant, "t1");
server.addInverseSearchQuery(rule2);
InverseSearch inverseSearch = Search.inverseSearch(resource1).setQueryFilter(tenant.equals("t1"));
InverseSearchResult result = server.execute(inverseSearch, testDocsFactory);
assertEquals(2, result.getNumOfResults());
inverseSearch = Search.inverseSearch(resource2).setQueryFilter(tenant.equals("t1"));
result = server.execute(inverseSearch, testDocsFactory);
assertEquals(1, result.getNumOfResults());
}
use of com.rbmhtechnology.vind.api.result.InverseSearchResult in project vind by RBMHTechnology.
the class ElasticSearchServer method execute.
@Override
public InverseSearchResult execute(InverseSearch inverseSearch, DocumentFactory documentFactory) {
final StopWatch elapsedtime = StopWatch.createStarted();
final List<Map<String, Object>> mapDocs = inverseSearch.getDocs().parallelStream().map(DocumentUtil::createInputDocument).collect(Collectors.toList());
final QueryBuilder query = ElasticQueryBuilder.buildFilterQuery(inverseSearch.getQueryFilter(), documentFactory, null, currentFootprint);
// query
try {
elasticClientLogger.debug(">>> query({})", query.toString());
// TODO: inverse search support multiple docs
final SearchResponse response = elasticSearchClient.percolatorDocQuery(mapDocs, query);
if (Objects.nonNull(response) && Objects.nonNull(response.getHits()) && Objects.nonNull(response.getHits().getHits())) {
final List<InverseSearchQuery> resultQueries = Arrays.stream(response.getHits().getHits()).map(hit -> DocumentUtil.buildVindDoc(hit, InverseSearchQueryFactory.getQueryFactory(), null)).map(doc -> documentFactory.createInverseSearchQuery(doc.getId(), deserializeByteArrayFilter(((ByteBuffer) doc.getValue(InverseSearchQueryFactory.BINARY_QUERY_FIELD)).array()))).collect(Collectors.toList());
elapsedtime.stop();
final long totalHits = response.getHits().getTotalHits().value;
switch(inverseSearch.getResultSet().getType()) {
case page:
{
return new InverseSearchPageResult(totalHits, response.getTook().getMillis(), resultQueries, inverseSearch, this, documentFactory).setElapsedTime(elapsedtime.getTime());
}
case slice:
{
return new InverseSearchSliceResult(totalHits, response.getTook().getMillis(), resultQueries, inverseSearch, this, documentFactory).setElapsedTime(elapsedtime.getTime());
}
default:
return new InverseSearchPageResult(totalHits, response.getTook().getMillis(), resultQueries, inverseSearch, this, documentFactory).setElapsedTime(elapsedtime.getTime());
}
} else {
throw new ElasticsearchException("Empty result from ElasticClient");
}
} catch (ElasticsearchException | IOException e) {
throw new SearchServerException(String.format("Cannot issue inverse search: %s", e.getMessage()), e);
}
}
use of com.rbmhtechnology.vind.api.result.InverseSearchResult in project vind by RBMHTechnology.
the class InverseSearchTest method testInverseSearch.
@Test
@RunWithBackend(Elastic)
public void testInverseSearch() {
final SearchServer server = testBackend.getSearchServer();
final SingleValueFieldDescriptor.TextFieldDescriptor title = new FieldDescriptorBuilder().setFullText(true).setFacet(true).buildTextField("title");
final SingleValueFieldDescriptor.TextFieldDescriptor volume = new FieldDescriptorBuilder().setFullText(true).setFacet(true).buildTextField("volume");
final DocumentFactory testDocsFactory = new DocumentFactoryBuilder("TestDocument").addField(title).addInverseSearchMetaField(volume).build();
// Reverse Search
Document d1 = testDocsFactory.createDoc("1").setValue(title, "Hello World");
final InverseSearchQuery inverseSearchQuery = testDocsFactory.createInverseSearchQuery("testQuery1", title.equals("Hello World")).setValue(volume, "volume1");
final IndexResult indexResult = server.addInverseSearchQuery(inverseSearchQuery);
InverseSearch inverseSearch = Search.inverseSearch(d1).setQueryFilter(volume.equals("volume1"));
InverseSearchResult result = server.execute(inverseSearch, testDocsFactory);
assertEquals(1, result.getNumOfResults());
inverseSearch = Search.inverseSearch(d1).setQueryFilter(volume.equals("v1"));
result = server.execute(inverseSearch, testDocsFactory);
assertEquals(0, result.getNumOfResults());
}
Aggregations