/** * A search clause that will match inputs that had metadata that matches the metadata on the given * {@link ClarifaiInput} * * @param input the input whose metadata to search by * @return a {@link SearchClause} to be given to a search endpoint * @see #matchMetadata(JsonObject) */ @NotNull public static SearchClause matchMetadata(@NotNull ClarifaiInput input) { return matchMetadata(input.metadata()); }
@Test public void searchANDing() { client.searchInputs() .ands( SearchClause.matchUserTaggedConcept(Concept.forName("cat")), SearchClause.matchConcept(Concept.forName("dog").withValue(false)), SearchClause.matchImageVisually(ClarifaiImage.of("@@sampleTrain")) ) .getPage(1) .executeSync(); }
@NotNull @Override protected ListenableFuture buildRequestGrpc(int page) { Search.Query.Builder searchBuilder = Search.Query.newBuilder(); for (SearchClause andClause : andClauses) { searchBuilder.addAnds(andClause.serialize()); } if (language != null) { searchBuilder.setLanguage(language); } return stub(page).postSearches(Search.PostSearchesRequest.newBuilder().setQuery(searchBuilder).build()); } }
@Test public void t17e_searchInputsWithModel_geo() { assertSuccess(client.addInputs().plus( ClarifaiInput.forImage(METRO_NORTH_IMAGE_URL) .withGeo(PointF.at(90F, 23F)) )); assertSuccess( client.searchInputs(matchConcept(Concept.forID("outdoors23").withValue(true))) .and(SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL))) .and(SearchClause.matchGeo(PointF.at(90F, 23F), Radius.of(5, Radius.Unit.MILE))) .build() ); }
@Test public void searchByConceptAndPrediction() { client.searchInputs() // Matches images we tagged as "cat", and that the API tagged as not having "dog" .ands( SearchClause.matchUserTaggedConcept(Concept.forName("cat")), SearchClause.matchConcept(Concept.forName("dog").withValue(false)) ) .getPage(1) .executeSync(); }
@Retry @Test public void t17b_searchInputsWithModel_complexSearch() { assertSuccess( client.searchInputs(matchConcept(Concept.forID("outdoors23").withValue(true))) .and(SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL))) .build() ); }
@Retry @Test public void t17a_searchInputsWithModel() { assertSuccess(client.searchInputs( SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL)) )); }
@Test public void searchByReverseImage() { // Search by image (image URL represented by: String or java.net.URL) client.searchInputs(SearchClause.matchImageVisually(ClarifaiImage.of("@@sampleTrain"))) .getPage(1) .executeSync(); // Search by image (image represented by: java.io.File or byte[]) client.searchInputs(SearchClause.matchImageVisually(ClarifaiImage.of(new File("image.png")))) .getPage(1) .executeSync(); }
@Test public void searchByPredictedConcepts() { // Search concept by name client.searchInputs(SearchClause.matchConcept(Concept.forName("cat"))) .getPage(1) .executeSync(); // Search concept by ID client.searchInputs(SearchClause.matchConcept(Concept.forID("ai_mFqxrph2"))) .getPage(1) .executeSync(); // Search multiple concepts client.searchInputs(SearchClause.matchConcept(Concept.forID("cat"))) .and(SearchClause.matchConcept(Concept.forID("cute"))) .getPage(1) .executeSync(); // Search NOT by concept client.searchInputs(SearchClause.matchConcept(Concept.forID("cat").withValue(false))) .getPage(1) .executeSync(); }
@Test public void searchByUserSuppliedConcept() { // Search concept by name client.searchInputs(SearchClause.matchUserTaggedConcept(Concept.forName("cat"))) .getPage(1) .executeSync(); // Search concept by ID client.searchInputs(SearchClause.matchUserTaggedConcept(Concept.forID("ai_mFqxrph2"))) .getPage(1) .executeSync(); // Search multiple concepts client.searchInputs(SearchClause.matchUserTaggedConcept(Concept.forID("cat"))) .and(SearchClause.matchUserTaggedConcept(Concept.forID("cute"))) .getPage(1) .executeSync(); // Search NOT by concept client.searchInputs(SearchClause.matchUserTaggedConcept(Concept.forID("cat").withValue(false))) .getPage(1) .executeSync(); }
@Retry @Test public void t18_testGeo() { { final List<SearchHit> hitsBeforeAdding = assertSuccess( client.searchInputs(SearchClause.matchGeo(PointF.at(59F, 29.75F), Radius.of(500, Radius.Unit.MILE))) ).searchHits(); assertEquals(0, hitsBeforeAdding.size()); } assertSuccess(client.addInputs().plus( ClarifaiInput.forImage(KOTLIN_LOGO_IMAGE_FILE) .withGeo(PointF.at(60F, 29.75F)) )); { final List<SearchHit> hitsAfterAdding = assertSuccess( client.searchInputs(SearchClause.matchGeo(PointF.at(59F, 29.75F), Radius.of(500, Radius.Unit.MILE))) ).searchHits(); assertEquals(1, hitsAfterAdding.size()); } { final List<SearchHit> hits = assertSuccess( client.searchInputs(SearchClause.matchGeo(PointF.at(3F, 0F), PointF.at(70, 30F))) ).searchHits(); assertEquals(1, hits.size()); } }
@Test public void searchMatchURL() { // Lookup images with this URL client.searchInputs(SearchClause.matchImageURL(ClarifaiImage.of(""))) .getPage(1) .executeSync(); }
@Test public void reverseImageSearch() { client.searchInputs(SearchClause.matchImageVisually(ClarifaiImage.of("http://i.imgur.com/HEoT5xR.png"))) .getPage(1) .executeSync(); }
@Test public void searchByConcept() { client.searchInputs(SearchClause.matchConcept(Concept.forName("dress"))) .build() .getPage(1) .executeSync(); }
@Retry @Test public void t17c_searchInputsWithModel_metadata() { final List<SearchHit> hits = assertSuccess( client.searchInputs(SearchClause.matchMetadata(new JSONObjectBuilder().add("foo", "bar").build())) ).searchHits(); final ClarifaiInput hit = hits.stream() .filter(someHit -> "inputWithMetadata".equals(someHit.input().id())) .findFirst() .orElseThrow(() -> new AssertionError("")) .input(); assertEquals("inputWithMetadata", hit.id()); assertEquals(new JSONObjectBuilder().add("foo", "bar").build(), hit.metadata()); }
@Retry @Test public void t17d_searchInputsWithModel_multi_language() { assertSuccess(client.searchInputs( SearchClause.matchImageURL(ClarifaiImage.of(METRO_NORTH_IMAGE_URL))).withLanguage("zh")); }
@Test public void quickStartSearch() { final ClarifaiResponse<SearchInputsResult> trainImages = client.searchInputs( // Finds images that match this picture of a train OR images that match the "train" concept tag SearchClause.matchImageVisually(ClarifaiImage.of("@@sampleTrain")) ) .getPage(1) .executeSync(); }
@Test public void allDataShouldBeExposedAndNotNull() { SearchInputsRequest request = client.searchInputs(SearchClause.matchImageURL( ClarifaiImage.of(imageWithNoSearchHits))); ClarifaiResponse<SearchInputsResult> response = request.getPage(1).executeSync(); assertNotNull(response.get().id()); assertNotNull(response.getStatus()); assertNotNull(response.get().searchHits()); } }