@Test
public void testFieldQueryWithSimilarityConstraint() throws YardException {
// NOTE: this does not test if the updated view of the representation is
// stored, but only that the update method works correctly
Yard yard = getYard();
String id1 = "urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1";
String id2 = "urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2";
String id3 = "urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id3";
String similarityfield = NamespaceEnum.rdfs+"comment";
String filterfield = "urn:the.field:used.for.testFieldQueryWithSimilarityConstraint.filter";
Representation test1 = create(id1, true);
Representation test2 = create(id2, true);
Representation test3 = create(id3, true);
// change the representations to be sure to force an update even if the
// implementation checks for changes before updating a representation
test1.add(similarityfield, "aaaa aaaa aaaa bbbb bbbb cccc cccc dddd dddd");
test1.add(filterfield, "Some text content");
test2.add(similarityfield, "aaaa bbbb bbbb bbbb bbbb eeee");
test2.add(filterfield, "Some other content");
test3.add(similarityfield, "eeee eeee ffff gggg");
test3.add(filterfield, "Different content");
Iterable<Representation> updatedIterable = yard.update(Arrays.asList(test1, test2, test3));
assertNotNull(updatedIterable);
// Perform a first similarity query that looks a lot like the first document
FieldQuery query = yard.getQueryFactory().createFieldQuery();
query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
QueryResultList<Representation> results = yard.find(query);
assertEquals(2, results.size());
Iterator<Representation> it = results.iterator();
Representation first = it.next();
assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id1", first.getId());
// assertEquals(0.99, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));
Representation second = it.next();
assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2",
second.getId());
// assertEquals(0.80, first.getFirst("http://www.iks-project.eu/ontology/rick/query/score"));
// combine similarity with traditional filtering
query = yard.getQueryFactory().createFieldQuery();
query.setConstraint(similarityfield, new SimilarityConstraint("aaaa aaaa aaaa aaaa zzzz yyyy"));
query.setConstraint(filterfield, new TextConstraint(Arrays.asList("other")));
results = yard.find(query);
assertEquals(1, results.size());
it = results.iterator();
first = it.next();
assertEquals("urn:yard.test.testFieldQueryWithSimilarityConstraint:representation.id2", first.getId());
}