use of org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException in project neo4j by neo4j.
the class FulltextIndexProviderTest method shouldAnswerExactIfCypherCompatible.
@Test
void shouldAnswerExactIfCypherCompatible() throws KernelException {
IndexDescriptor indexReference;
Label containsLabel = label("containsLabel");
String containsProp = "containsProp";
long nodea;
long nodeapa1;
long nodeapa2;
long nodeapaapa;
try (Transaction tx = db.beginTx()) {
nodea = createNode(tx, containsLabel, containsProp, "a");
createNode(tx, containsLabel, containsProp, "A");
nodeapa1 = createNode(tx, containsLabel, containsProp, "apa");
nodeapa2 = createNode(tx, containsLabel, containsProp, "apa");
createNode(tx, containsLabel, containsProp, "longapa");
nodeapaapa = createNode(tx, containsLabel, containsProp, "apa apa");
createNode(tx, containsLabel, containsProp, "apalong");
tx.commit();
}
int containsLabelId;
int containsPropertyId;
try (Transaction tx = db.beginTx()) {
TokenRead tokenRead = tokenRead(tx);
containsLabelId = tokenRead.nodeLabel(containsLabel.name());
containsPropertyId = tokenRead.propertyKey(containsProp);
}
indexReference = createIndex(new int[] { containsLabelId }, new int[] { containsPropertyId }, "cypher");
await(indexReference);
try (Transaction tx = db.beginTx()) {
KernelTransaction ktx = LuceneFulltextTestSupport.kernelTransaction(tx);
assertQueryResult(ktx, exactQuery(containsPropertyId, "a"), nodea);
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa"), nodeapa1, nodeapa2);
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa*"));
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa a"));
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa apa"), nodeapaapa);
assertQueryResult(ktx, exactQuery(containsPropertyId, "*apa"));
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa*"));
IndexNotApplicableKernelException e = assertThrows(IndexNotApplicableKernelException.class, () -> assertQueryResult(ktx, exactQuery(containsPropertyId, 1)));
assertThat(e.getMessage()).contains("A fulltext schema index cannot answer " + PropertyIndexQuery.IndexQueryType.exact + " queries on " + ValueCategory.NUMBER + " values.");
}
controller.restartDbms();
try (Transaction tx = db.beginTx()) {
KernelTransaction ktx = LuceneFulltextTestSupport.kernelTransaction(tx);
assertQueryResult(ktx, exactQuery(containsPropertyId, "a"), nodea);
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa"), nodeapa1, nodeapa2);
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa*"));
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa a"));
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa apa"), nodeapaapa);
assertQueryResult(ktx, exactQuery(containsPropertyId, "*apa"));
assertQueryResult(ktx, exactQuery(containsPropertyId, "apa*"));
IndexNotApplicableKernelException e = assertThrows(IndexNotApplicableKernelException.class, () -> assertQueryResult(ktx, exactQuery(containsPropertyId, 1)));
assertThat(e.getMessage()).contains("A fulltext schema index cannot answer " + PropertyIndexQuery.IndexQueryType.exact + " queries on " + ValueCategory.NUMBER + " values.");
}
}
use of org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException in project neo4j by neo4j.
the class FulltextIndexProviderTest method shouldThrowOnCypherFulltextQueryIfNotCypherCompatibleNotNodeIndex.
@Test
void shouldThrowOnCypherFulltextQueryIfNotCypherCompatibleNotNodeIndex() throws KernelException {
IndexDescriptor indexReference = createIndex(new int[] { relTypeIdHej }, new int[] { propIdHej }, "cypher", EntityType.RELATIONSHIP);
await(indexReference);
try (Transaction tx = db.beginTx()) {
KernelTransaction ktx = LuceneFulltextTestSupport.kernelTransaction(tx);
IndexNotApplicableKernelException e = assertThrows(IndexNotApplicableKernelException.class, () -> assertQueryResult(ktx, endsWithQuery(1, "a")));
assertThat(e.getMessage()).contains("Node index seek can only be performed on node indexes");
}
}
use of org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException in project neo4j by neo4j.
the class FulltextIndexReader method query.
@Override
public void query(QueryContext context, IndexProgressor.EntityValueClient client, IndexQueryConstraints constraints, PropertyIndexQuery... queries) throws IndexNotApplicableKernelException {
BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
for (PropertyIndexQuery indexQuery : queries) {
if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.fulltextSearch) {
PropertyIndexQuery.FulltextSearchPredicate fulltextSearch = (PropertyIndexQuery.FulltextSearchPredicate) indexQuery;
try {
queryBuilder.add(parseFulltextQuery(fulltextSearch.query()), BooleanClause.Occur.SHOULD);
} catch (ParseException e) {
throw new RuntimeException("Could not parse the given fulltext search query: '" + fulltextSearch.query() + "'.", e);
}
} else {
// Not fulltext query
assertNotComposite(queries);
assertCypherCompatible();
Query query;
if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringContains) {
PropertyIndexQuery.StringContainsPredicate scp = (PropertyIndexQuery.StringContainsPredicate) indexQuery;
String searchTerm = QueryParser.escape(scp.contains().stringValue());
Term term = new Term(propertyNames[0], "*" + searchTerm + "*");
query = new WildcardQuery(term);
} else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringSuffix) {
PropertyIndexQuery.StringSuffixPredicate ssp = (PropertyIndexQuery.StringSuffixPredicate) indexQuery;
String searchTerm = QueryParser.escape(ssp.suffix().stringValue());
Term term = new Term(propertyNames[0], "*" + searchTerm);
query = new WildcardQuery(term);
} else if (indexQuery.type() == PropertyIndexQuery.IndexQueryType.stringPrefix) {
PropertyIndexQuery.StringPrefixPredicate spp = (PropertyIndexQuery.StringPrefixPredicate) indexQuery;
String searchTerm = spp.prefix().stringValue();
Term term = new Term(propertyNames[0], searchTerm);
query = new LuceneDocumentStructure.PrefixMultiTermsQuery(term);
} else if (indexQuery.getClass() == PropertyIndexQuery.ExactPredicate.class && indexQuery.valueGroup() == ValueGroup.TEXT) {
PropertyIndexQuery.ExactPredicate exact = (PropertyIndexQuery.ExactPredicate) indexQuery;
String searchTerm = ((TextValue) exact.value()).stringValue();
Term term = new Term(propertyNames[0], searchTerm);
query = new ConstantScoreQuery(new TermQuery(term));
} else if (indexQuery.getClass() == PropertyIndexQuery.TextRangePredicate.class) {
PropertyIndexQuery.TextRangePredicate sp = (PropertyIndexQuery.TextRangePredicate) indexQuery;
query = newRangeSeekByStringQuery(propertyNames[0], sp.from(), sp.fromInclusive(), sp.to(), sp.toInclusive());
} else {
throw new IndexNotApplicableKernelException("A fulltext schema index cannot answer " + indexQuery.type() + " queries on " + indexQuery.valueCategory() + " values.");
}
queryBuilder.add(query, BooleanClause.Occur.MUST);
}
}
Query query = queryBuilder.build();
ValuesIterator itr = searchLucene(query, constraints, context, context.cursorContext(), context.memoryTracker());
IndexProgressor progressor = new FulltextIndexProgressor(itr, client, constraints);
client.initialize(index, progressor, queries, constraints, true);
}
use of org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException in project neo4j by neo4j.
the class NativeIndexAccessorTests method getValues.
@Test
void getValues() throws IndexEntryConflictException, IndexNotApplicableKernelException {
// given
int nUpdates = 10000;
Iterator<ValueIndexEntryUpdate<IndexDescriptor>> randomUpdateGenerator = valueCreatorUtil.randomUpdateGenerator(random);
// noinspection unchecked
ValueIndexEntryUpdate<IndexDescriptor>[] someUpdates = new ValueIndexEntryUpdate[nUpdates];
for (int i = 0; i < nUpdates; i++) {
someUpdates[i] = randomUpdateGenerator.next();
}
processAll(someUpdates);
Value[] allValues = ValueCreatorUtil.extractValuesFromUpdates(someUpdates);
// Pick one out of all added values and do a range query for the value group of that value
Value value = random.among(allValues);
ValueGroup valueGroup = value.valueGroup();
IndexValueCapability valueCapability = indexCapability().valueCapability(valueGroup.category());
if (!valueCapability.equals(IndexValueCapability.YES)) {
// We don't need to do this test
return;
}
PropertyIndexQuery.RangePredicate<?> supportedQuery;
List<Value> expectedValues;
if (Values.isGeometryValue(value)) {
// Unless it's a point value in which case we query for the specific coordinate reference system instead
CoordinateReferenceSystem crs = ((PointValue) value).getCoordinateReferenceSystem();
supportedQuery = PropertyIndexQuery.range(0, crs);
expectedValues = Arrays.stream(allValues).filter(v -> v.valueGroup() == ValueGroup.GEOMETRY).filter(v -> ((PointValue) v).getCoordinateReferenceSystem() == crs).collect(Collectors.toList());
} else {
supportedQuery = PropertyIndexQuery.range(0, valueGroup);
expectedValues = Arrays.stream(allValues).filter(v -> v.valueGroup() == valueGroup).collect(Collectors.toList());
}
// when
try (var reader = accessor.newValueReader()) {
SimpleEntityValueClient client = new SimpleEntityValueClient();
reader.query(NULL_CONTEXT, client, unorderedValues(), supportedQuery);
// then
while (client.next()) {
Value foundValue = client.values[0];
assertTrue(expectedValues.remove(foundValue), "found value that was not expected " + foundValue);
}
assertThat(expectedValues.size()).as("did not find all expected values").isEqualTo(0);
}
}
use of org.neo4j.internal.kernel.api.exceptions.schema.IndexNotApplicableKernelException in project neo4j by neo4j.
the class PartitionedValueIndexReader method query.
@Override
public void query(QueryContext context, IndexProgressor.EntityValueClient client, IndexQueryConstraints constraints, PropertyIndexQuery... query) throws IndexNotApplicableKernelException {
try {
BridgingIndexProgressor bridgingIndexProgressor = new BridgingIndexProgressor(client, descriptor.schema().getPropertyIds());
indexReaders.parallelStream().forEach(reader -> {
try {
reader.query(context, bridgingIndexProgressor, constraints, query);
} catch (IndexNotApplicableKernelException e) {
throw new InnerException(e);
}
});
client.initialize(descriptor, bridgingIndexProgressor, query, constraints, false);
} catch (InnerException e) {
throw e.getCause();
}
}
Aggregations