use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class LuceneMatchListener method reset.
protected void reset(final DBBroker broker, final NodeProxy proxy) {
this.broker = broker;
this.match = proxy.getMatches();
setNextInChain(null);
final IndexSpec indexConf = proxy.getOwnerDocument().getCollection().getIndexConfiguration(broker);
if (indexConf != null) {
config = (LuceneConfig) indexConf.getCustomIndexSpec(LuceneIndex.ID);
} else {
config = LuceneConfig.DEFAULT_CONFIG;
}
getTerms();
nodesWithMatch = new TreeMap<>();
/* Check if an index is defined on an ancestor of the current node.
* If yes, scan the ancestor to get the offset of the first character
* in the current node. For example, if the indexed node is <a>abc<b>de</b></a>
* and we query for //a[text:ngram-contains(., 'de')]/b, proxy will be a <b> node, but
* the offsets of the matches are relative to the start of <a>.
*/
NodeSet ancestors = null;
Match nextMatch = this.match;
while (nextMatch != null) {
if (proxy.getNodeId().isDescendantOf(nextMatch.getNodeId())) {
if (ancestors == null) {
ancestors = new NewArrayNodeSet();
}
ancestors.add(new NodeProxy(proxy.getOwnerDocument(), nextMatch.getNodeId()));
}
nextMatch = nextMatch.getNextMatch();
}
if (ancestors != null && !ancestors.isEmpty()) {
for (final NodeProxy p : ancestors) {
scanMatches(p);
}
}
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class LuceneMatchListener method getTerms.
/**
* Get all query terms from the original queries.
*/
private void getTerms() {
try {
index.withReader(reader -> {
final Set<Query> queries = new HashSet<>();
termMap = new TreeMap<>();
Match nextMatch = this.match;
while (nextMatch != null) {
if (nextMatch.getIndexId().equals(LuceneIndex.ID)) {
final Query query = ((LuceneMatch) nextMatch).getQuery();
if (!queries.contains(query)) {
queries.add(query);
LuceneUtil.extractTerms(query, termMap, reader, false);
}
}
nextMatch = nextMatch.getNextMatch();
}
return null;
});
} catch (final IOException e) {
LOG.warn("Match listener caught IO exception while reading query tersm: {}", e.getMessage(), e);
}
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class Score method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
NodeValue nodeValue = (NodeValue) args[0].itemAt(0);
if (nodeValue.getImplementationType() != NodeValue.PERSISTENT_NODE) {
return Sequence.EMPTY_SEQUENCE;
}
NodeProxy proxy = (NodeProxy) nodeValue;
Match match = proxy.getMatches();
float score = 0.0f;
while (match != null) {
if (match.getIndexId().equals(LuceneIndex.ID)) {
float currentScore = ((LuceneMatch) match).getScore();
score += currentScore;
}
match = match.getNextMatch();
}
return new FloatValue(score);
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class NGramSearch method getContinuousMatches.
/**
* Finds all matches in head which are followed by matches in tail in the specified distance.
*
* @param head
* a nodeset with matches
* @param tail
* another nodeset with matches
* @return a nodeset containing all matches from the head which are directly followed by matches in the tail
*/
private NodeProxy getContinuousMatches(final NodeProxy head, final NodeProxy tail) {
// NodeSet result = new ExtArrayNodeSet();
Match continuousMatch = null;
Match headMatch = head.getMatches();
while (headMatch != null && continuousMatch == null) {
Match tailMatch = tail.getMatches();
while (tailMatch != null && continuousMatch == null) {
continuousMatch = headMatch.continuedBy(tailMatch);
tailMatch = tailMatch.getNextMatch();
}
headMatch = headMatch.getNextMatch();
}
if (continuousMatch != null) {
NodeProxies.filterMatches(tail, match -> match.getContextId() != getExpressionId());
tail.addMatch(continuousMatch);
return tail;
}
return null;
}
use of org.exist.dom.persistent.Match in project exist by eXist-db.
the class NGramSearch method processMatches.
private NodeSet processMatches(NGramIndexWorker index, DocumentSet docs, List<QName> qnames, String query, NodeSet nodeSet, int axis) throws XPathException {
EvaluatableExpression parsedQuery = null;
if (getLocalName().equals("wildcard-contains"))
parsedQuery = parseQuery(query);
else
parsedQuery = new FixedString(this, query);
LOG.debug("Parsed Query: {}", parsedQuery);
NodeSet result = parsedQuery.eval(index, docs, qnames, nodeSet, axis, this.getExpressionId());
if (getLocalName().startsWith("starts-with"))
result = NodeSets.getNodesMatchingAtStart(result, getExpressionId());
else if (getLocalName().startsWith("ends-with"))
result = NodeSets.getNodesMatchingAtEnd(result, getExpressionId());
result = NodeSets.transformNodes(result, proxy -> NodeProxies.transformOwnMatches(proxy, Match::filterOutOverlappingOffsets, getExpressionId()));
return result;
}
Aggregations