-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remaining queries for script keyword fields (#59630)
Adds the remaining queries for scripted keyword fields and handles a few leftover TODOs, mostly around `hashCode` and `equals`.
- Loading branch information
Showing
22 changed files
with
1,269 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
.../org/elasticsearch/xpack/runtimefields/query/AbstractStringScriptFieldAutomatonQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.query; | ||
|
||
import org.apache.lucene.search.QueryVisitor; | ||
import org.apache.lucene.util.BytesRefBuilder; | ||
import org.apache.lucene.util.automaton.ByteRunAutomaton; | ||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.xpack.runtimefields.StringScriptFieldScript; | ||
|
||
import java.util.List; | ||
|
||
public abstract class AbstractStringScriptFieldAutomatonQuery extends AbstractStringScriptFieldQuery { | ||
private final BytesRefBuilder scratch = new BytesRefBuilder(); | ||
private final ByteRunAutomaton automaton; | ||
|
||
public AbstractStringScriptFieldAutomatonQuery( | ||
Script script, | ||
StringScriptFieldScript.LeafFactory leafFactory, | ||
String fieldName, | ||
ByteRunAutomaton automaton | ||
) { | ||
super(script, leafFactory, fieldName); | ||
this.automaton = automaton; | ||
} | ||
|
||
@Override | ||
protected final boolean matches(List<String> values) { | ||
for (String value : values) { | ||
scratch.copyChars(value); | ||
if (automaton.run(scratch.bytes(), 0, scratch.length())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public final void visit(QueryVisitor visitor) { | ||
if (visitor.acceptField(fieldName())) { | ||
visitor.consumeTermsMatching(this, fieldName(), () -> automaton); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...rc/main/java/org/elasticsearch/xpack/runtimefields/query/StringScriptFieldFuzzyQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.query; | ||
|
||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.search.FuzzyQuery; | ||
import org.apache.lucene.util.automaton.ByteRunAutomaton; | ||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.xpack.runtimefields.StringScriptFieldScript; | ||
|
||
import java.util.Objects; | ||
|
||
public class StringScriptFieldFuzzyQuery extends AbstractStringScriptFieldAutomatonQuery { | ||
public static StringScriptFieldFuzzyQuery build( | ||
Script script, | ||
StringScriptFieldScript.LeafFactory leafFactory, | ||
String fieldName, | ||
String term, | ||
int maxEdits, | ||
int prefixLength, | ||
boolean transpositions | ||
) { | ||
int maxExpansions = 1; // We don't actually expand anything so the value here doesn't matter | ||
FuzzyQuery delegate = new FuzzyQuery(new Term(fieldName, term), maxEdits, prefixLength, maxExpansions, transpositions); | ||
ByteRunAutomaton automaton = delegate.getAutomata().runAutomaton; | ||
return new StringScriptFieldFuzzyQuery(script, leafFactory, fieldName, automaton, delegate); | ||
} | ||
|
||
private final FuzzyQuery delegate; | ||
|
||
private StringScriptFieldFuzzyQuery( | ||
Script script, | ||
StringScriptFieldScript.LeafFactory leafFactory, | ||
String fieldName, | ||
ByteRunAutomaton automaton, | ||
FuzzyQuery delegate | ||
) { | ||
super(script, leafFactory, fieldName, automaton); | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public final String toString(String field) { | ||
return delegate.toString(field); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), delegate); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (false == super.equals(obj)) { | ||
return false; | ||
} | ||
StringScriptFieldFuzzyQuery other = (StringScriptFieldFuzzyQuery) obj; | ||
return delegate.equals(other.delegate); | ||
} | ||
|
||
FuzzyQuery delegate() { | ||
return delegate; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...c/main/java/org/elasticsearch/xpack/runtimefields/query/StringScriptFieldPrefixQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.runtimefields.query; | ||
|
||
import org.apache.lucene.search.PrefixQuery; | ||
import org.apache.lucene.search.QueryVisitor; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.apache.lucene.util.automaton.ByteRunAutomaton; | ||
import org.elasticsearch.script.Script; | ||
import org.elasticsearch.xpack.runtimefields.StringScriptFieldScript; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class StringScriptFieldPrefixQuery extends AbstractStringScriptFieldQuery { | ||
private final String prefix; | ||
|
||
public StringScriptFieldPrefixQuery(Script script, StringScriptFieldScript.LeafFactory leafFactory, String fieldName, String prefix) { | ||
super(script, leafFactory, fieldName); | ||
this.prefix = Objects.requireNonNull(prefix); | ||
} | ||
|
||
@Override | ||
protected boolean matches(List<String> values) { | ||
for (String value : values) { | ||
if (value != null && value.startsWith(prefix)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void visit(QueryVisitor visitor) { | ||
if (visitor.acceptField(fieldName())) { | ||
visitor.consumeTermsMatching(this, fieldName(), () -> new ByteRunAutomaton(PrefixQuery.toAutomaton(new BytesRef(prefix)))); | ||
} | ||
} | ||
|
||
@Override | ||
public final String toString(String field) { | ||
if (fieldName().contentEquals(field)) { | ||
return prefix + "*"; | ||
} | ||
return fieldName() + ":" + prefix + "*"; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), prefix); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (false == super.equals(obj)) { | ||
return false; | ||
} | ||
StringScriptFieldPrefixQuery other = (StringScriptFieldPrefixQuery) obj; | ||
return prefix.equals(other.prefix); | ||
} | ||
|
||
String prefix() { | ||
return prefix; | ||
} | ||
} |
Oops, something went wrong.