Skip to content

Commit

Permalink
rename _shard -> _index and also rename classes and variables
Browse files Browse the repository at this point in the history
  • Loading branch information
brwe committed Jan 3, 2014
1 parent 058e90b commit 69a4896
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 157 deletions.
38 changes: 19 additions & 19 deletions docs/reference/modules/advanced-scripting.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
== Text scoring in scripts


Text features, such as term or document frequency for a specific term can be accessed in scripts (see <<modules-scripting, scripting documentation>> ) with the `_shard` variable. This can be useful if, for example, you want to implement your own scoring model using for example a script inside a <<query-dsl-function-score-query,function score query>>.
Text features, such as term or document frequency for a specific term can be accessed in scripts (see <<modules-scripting, scripting documentation>> ) with the `_index` variable. This can be useful if, for example, you want to implement your own scoring model using for example a script inside a <<query-dsl-function-score-query,function score query>>.
Statistics over the document collection are computed *per shard*, not per
index.

Expand Down Expand Up @@ -35,15 +35,15 @@ depending on the shard the current document resides in.
[float]
=== Shard statistics:

`_shard.numDocs()`::
`_index.numDocs()`::

Number of documents in shard.

`_shard.maxDoc()`::
`_index.maxDoc()`::

Maximal document number in shard.

`_shard.numDeletedDocs()`::
`_index.numDeletedDocs()`::

Number of deleted documents in shard.

Expand All @@ -52,49 +52,49 @@ depending on the shard the current document resides in.
=== Field statistics:

Field statistics can be accessed with a subscript operator like this:
`_shard['FIELD']`.
`_index['FIELD']`.


`_shard['FIELD'].docCount()`::
`_index['FIELD'].docCount()`::

Number of documents containing the field `FIELD`. Does not take deleted documents into account.

`_shard['FIELD'].sumttf()`::
`_index['FIELD'].sumttf()`::

Sum of `ttf` over all terms that appear in field `FIELD` in all documents.

`_shard['FIELD'].sumdf()`::
`_index['FIELD'].sumdf()`::

The sum of `df` s over all terms that appear in field `FIELD` in all
documents.


Field statistics are computed per shard and therfore these numbers can vary
depending on the shard the current document resides in.
The number of terms in a field cannot be accessed using the `_shard` variable. See <<mapping-core-types, word count mapping type>> on how to do that.
The number of terms in a field cannot be accessed using the `_index` variable. See <<mapping-core-types, word count mapping type>> on how to do that.

[float]
=== Term statistics:

Term statistics for a field can be accessed with a subscript operator like
this: `_shard['FIELD']['TERM']`. This will never return null, even if term or field does not exist.
If you do not need the term frequency, call `_shard['FIELD'].get('TERM', 0)`
this: `_index['FIELD']['TERM']`. This will never return null, even if term or field does not exist.
If you do not need the term frequency, call `_index['FIELD'].get('TERM', 0)`
to avoid uneccesary initialization of the frequencies. The flag will have only
affect is your set the `index_options` to `docs` (see <<mapping-core-types, mapping documentation>>).


`_shard['FIELD']['TERM'].df()`::
`_index['FIELD']['TERM'].df()`::

`df` of term `TERM` in field `FIELD`. Will be returned, even if the term
is not present in the current document.

`_shard['FIELD']['TERM'].ttf()`::
`_index['FIELD']['TERM'].ttf()`::

The sum of term frequencys of term `TERM` in field `FIELD` over all
documents. Will be returned, even if the term is not present in the
current document.

`_shard['FIELD']['TERM'].tf()`::
`_index['FIELD']['TERM'].tf()`::

`tf` of term `TERM` in field `FIELD`. Will be 0 if the term is not present
in the current document.
Expand All @@ -104,7 +104,7 @@ affect is your set the `index_options` to `docs` (see <<mapping-core-types, mapp
=== Term positions, offsets and payloads:

If you need information on the positions of terms in a field, call
`_shard['FIELD'].get('TERM', flag)` where flag can be
`_index['FIELD'].get('TERM', flag)` where flag can be

[horizontal]
`_POSITIONS`:: if you need the positions of the term
Expand All @@ -119,7 +119,7 @@ example, the following will return an object holding the positions and payloads,
as well as all statistics:


`_shard['FIELD'].get('TERM', _POSITIONS | _PAYLOADS)`
`_index['FIELD'].get('TERM', _POSITIONS | _PAYLOADS)`


Positions can be accessed with an iterator that returns an object
Expand Down Expand Up @@ -164,7 +164,7 @@ Example: sums up all payloads for the term `foo`.

[source,mvel]
---------------------------------------------------------
termInfo = _shard['my_field'].get('foo',_PAYLOADS);
termInfo = _index['my_field'].get('foo',_PAYLOADS);
score = 0;
for (pos : termInfo) {
score = score + pos.payloadAsInt(0);
Expand All @@ -176,8 +176,8 @@ return score;
[float]
=== Term vectors:

The `_shard` variable can only be used to gather statistics for single terms. If you want to use information on all terms in a field, you must store the term vectors (set `term_vector` in the mapping as described in the <<mapping-core-types,mapping documentation>>). To access them, call
`_shard.getTermVectors()` to get a
The `_index` variable can only be used to gather statistics for single terms. If you want to use information on all terms in a field, you must store the term vectors (set `term_vector` in the mapping as described in the <<mapping-core-types,mapping documentation>>). To access them, call
`_index.getTermVectors()` to get a
https://lucene.apache.org/core/4_0_0/core/org/apache/lucene/index/Fields.html[Fields]
instance. This object can then be used as described in https://lucene.apache.org/core/4_0_0/core/org/apache/lucene/index/Fields.html[lucene doc] to iterate over fields and then for each field iterate over each term in the field.
The method will return null if the term vectors were not stored.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.elasticsearch.script;

import org.elasticsearch.search.lookup.ShardTermsLookup;
import org.elasticsearch.search.lookup.IndexLookup;

import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.search.Scorer;
Expand Down Expand Up @@ -93,8 +93,8 @@ protected final SourceLookup source() {
/**
* Allows to access statistics on terms and fields.
*/
protected final ShardTermsLookup shardTerms() {
return lookup.shardTerms();
protected final IndexLookup indexLookup() {
return lookup.indexLookup();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* */
public class CachedPositionIterator extends PositionIterator {

public CachedPositionIterator(ScriptTerm termInfo) {
super(termInfo);
public CachedPositionIterator(IndexFieldTerm indexFieldTerm) {
super(indexFieldTerm);
}

// all payloads of the term in the current document in one bytes array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
/**
* Script interface to all information regarding a field.
* */
public class ScriptTerms extends MinimalMap<String, ScriptTerm> {
public class IndexField extends MinimalMap<String, IndexFieldTerm> {

/*
* TermsInfo Objects that represent the Terms are stored in this map when
* requested. Information such as frequency, doc frequency and positions
* information can be retrieved from the TermInfo objects in this map.
*/
private final Map<String, ScriptTerm> terms = new HashMap<String, ScriptTerm>();
private final Map<String, IndexFieldTerm> terms = new HashMap<String, IndexFieldTerm>();

// the name of this field
private final String fieldName;
Expand All @@ -46,7 +46,7 @@ public class ScriptTerms extends MinimalMap<String, ScriptTerm> {
* The holds the current reader. We need it to populate the field
* statistics. We just delegate all requests there
*/
private ShardTermsLookup shardTermsLookup;
private IndexLookup indexLookup;

/*
* General field statistics such as number of documents containing the
Expand All @@ -58,7 +58,7 @@ public class ScriptTerms extends MinimalMap<String, ScriptTerm> {
* Uodate posting lists in all TermInfo objects
*/
void setReader(AtomicReader reader) {
for (ScriptTerm ti : terms.values()) {
for (IndexFieldTerm ti : terms.values()) {
ti.setNextReader(reader);
}
}
Expand All @@ -68,15 +68,15 @@ void setReader(AtomicReader reader) {
* statistics of this field. Information on specific terms in this field can
* be accessed by calling get(String term).
*/
public ScriptTerms(String fieldName, ShardTermsLookup shardTermsLookup) throws IOException {
public IndexField(String fieldName, IndexLookup indexLookup) throws IOException {

assert fieldName != null;
this.fieldName = fieldName;

assert shardTermsLookup != null;
this.shardTermsLookup = shardTermsLookup;
assert indexLookup != null;
this.indexLookup = indexLookup;

fieldStats = shardTermsLookup.getIndexSearcher().collectionStatistics(fieldName);
fieldStats = this.indexLookup.getIndexSearcher().collectionStatistics(fieldName);
}

/* get number of documents containing the field */
Expand Down Expand Up @@ -107,29 +107,29 @@ public long sumdf() throws IOException {
* advance which terms are requested, we could provide an array which the
* user could then iterate over.
*/
public ScriptTerm get(Object key, int flags) {
public IndexFieldTerm get(Object key, int flags) {
String termString = (String) key;
ScriptTerm termInfo = terms.get(termString);
IndexFieldTerm indexFieldTerm = terms.get(termString);
// see if we initialized already...
if (termInfo == null) {
termInfo = new ScriptTerm(termString, fieldName, shardTermsLookup, flags);
terms.put(termString, termInfo);
if (indexFieldTerm == null) {
indexFieldTerm = new IndexFieldTerm(termString, fieldName, indexLookup, flags);
terms.put(termString, indexFieldTerm);
}
termInfo.validateFlags(flags);
return termInfo;
indexFieldTerm.validateFlags(flags);
return indexFieldTerm;
}

/*
* Returns a TermInfo object that can be used to access information on
* specific terms. flags can be set as described in TermInfo.
*/
public ScriptTerm get(Object key) {
public IndexFieldTerm get(Object key) {
// per default, do not initialize any positions info
return get(key, ShardTermsLookup.FLAG_FREQUENCIES);
return get(key, IndexLookup.FLAG_FREQUENCIES);
}

public void setDocIdInTerms(int docId) {
for (ScriptTerm ti : terms.values()) {
for (IndexFieldTerm ti : terms.values()) {
ti.setNextDoc(docId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* Holds all information on a particular term in a field.
* */
public class ScriptTerm implements Iterable<TermPosition> {
public class IndexFieldTerm implements Iterable<TermPosition> {

// The posting list for this term. Is null if the term or field does not
// exist. Can be DocsEnum or DocsAndPositionsEnum.
Expand Down Expand Up @@ -90,16 +90,16 @@ void setNextReader(AtomicReader reader) {
}

private boolean shouldRetrieveFrequenciesOnly() {
return (flags & ~ShardTermsLookup.FLAG_FREQUENCIES) == 0;
return (flags & ~IndexLookup.FLAG_FREQUENCIES) == 0;
}

private int getLuceneFrequencyFlag(int flags) {
return (flags & ShardTermsLookup.FLAG_FREQUENCIES) > 0 ? DocsEnum.FLAG_FREQS : DocsEnum.FLAG_NONE;
return (flags & IndexLookup.FLAG_FREQUENCIES) > 0 ? DocsEnum.FLAG_FREQS : DocsEnum.FLAG_NONE;
}

private int getLucenePositionsFlags(int flags) {
int lucenePositionsFlags = (flags & ShardTermsLookup.FLAG_PAYLOADS) > 0 ? DocsAndPositionsEnum.FLAG_PAYLOADS : 0x0;
lucenePositionsFlags |= (flags & ShardTermsLookup.FLAG_OFFSETS) > 0 ? DocsAndPositionsEnum.FLAG_OFFSETS : 0x0;
int lucenePositionsFlags = (flags & IndexLookup.FLAG_PAYLOADS) > 0 ? DocsAndPositionsEnum.FLAG_PAYLOADS : 0x0;
lucenePositionsFlags |= (flags & IndexLookup.FLAG_OFFSETS) > 0 ? DocsAndPositionsEnum.FLAG_OFFSETS : 0x0;
return lucenePositionsFlags;
}

Expand Down Expand Up @@ -162,19 +162,19 @@ public void setNextDoc(int docId) {
}
iterator.nextDoc();
} catch (IOException e) {
throw new ElasticSearchException("While trying to initialize term positions in ScriptTerm.setNextDoc() ", e);
throw new ElasticSearchException("While trying to initialize term positions in IndexFieldTerm.setNextDoc() ", e);
}
}

public ScriptTerm(String term, String fieldName, ShardTermsLookup shardTermsLookup, int flags) {
public IndexFieldTerm(String term, String fieldName, IndexLookup indexLookup, int flags) {
assert fieldName != null;
this.fieldName = fieldName;
assert term != null;
this.term = term;
assert shardTermsLookup != null;
assert indexLookup != null;
identifier = new Term(fieldName, (String) term);
this.flags = flags;
boolean doRecord = ((flags & ShardTermsLookup.FLAG_CACHE) > 0);
boolean doRecord = ((flags & IndexLookup.FLAG_CACHE) > 0);
if (withPositions()) {
if (!doRecord) {
iterator = new PositionIterator(this);
Expand All @@ -184,11 +184,11 @@ public ScriptTerm(String term, String fieldName, ShardTermsLookup shardTermsLook
} else {
iterator = new PositionIterator(this);
}
setNextReader(shardTermsLookup.getReader());
setNextDoc(shardTermsLookup.getDocId());
setNextReader(indexLookup.getReader());
setNextDoc(indexLookup.getDocId());
try {
termStats = shardTermsLookup.getIndexSearcher().termStatistics(identifier,
TermContext.build(shardTermsLookup.getReaderContext(), identifier));
termStats = indexLookup.getIndexSearcher().termStatistics(identifier,
TermContext.build(indexLookup.getReaderContext(), identifier));
} catch (IOException e) {
throw new ElasticSearchException("Cannot get term statistics: ", e);
}
Expand All @@ -199,15 +199,15 @@ private boolean withPositions() {
}

protected boolean shouldRetrievePositions() {
return (flags & ShardTermsLookup.FLAG_POSITIONS) > 0;
return (flags & IndexLookup.FLAG_POSITIONS) > 0;
}

protected boolean shouldRetrieveOffsets() {
return (flags & ShardTermsLookup.FLAG_OFFSETS) > 0;
return (flags & IndexLookup.FLAG_OFFSETS) > 0;
}

protected boolean shouldRetrievePayloads() {
return (flags & ShardTermsLookup.FLAG_PAYLOADS) > 0;
return (flags & IndexLookup.FLAG_PAYLOADS) > 0;
}

public int tf() throws IOException {
Expand Down Expand Up @@ -241,24 +241,24 @@ private String getCalledStatement(int flags2) {
}

private String getCallStatement(String calledFlags) {
return "_shard['" + this.fieldName + "'].get('" + this.term + "', " + calledFlags + ")";
return "_index['" + this.fieldName + "'].get('" + this.term + "', " + calledFlags + ")";
}

private String getFlagsString(int flags2) {
String flagsString = null;
if ((flags2 & ShardTermsLookup.FLAG_FREQUENCIES) != 0) {
if ((flags2 & IndexLookup.FLAG_FREQUENCIES) != 0) {
flagsString = anddToFlagsString(flagsString, "_FREQUENCIES");
}
if ((flags2 & ShardTermsLookup.FLAG_POSITIONS) != 0) {
if ((flags2 & IndexLookup.FLAG_POSITIONS) != 0) {
flagsString = anddToFlagsString(flagsString, "_POSITIONS");
}
if ((flags2 & ShardTermsLookup.FLAG_OFFSETS) != 0) {
if ((flags2 & IndexLookup.FLAG_OFFSETS) != 0) {
flagsString = anddToFlagsString(flagsString, "_OFFSETS");
}
if ((flags2 & ShardTermsLookup.FLAG_PAYLOADS) != 0) {
if ((flags2 & IndexLookup.FLAG_PAYLOADS) != 0) {
flagsString = anddToFlagsString(flagsString, "_PAYLOADS");
}
if ((flags2 & ShardTermsLookup.FLAG_CACHE) != 0) {
if ((flags2 & IndexLookup.FLAG_CACHE) != 0) {
flagsString = anddToFlagsString(flagsString, "_CACHE");
}
return flagsString;
Expand Down

0 comments on commit 69a4896

Please sign in to comment.