Search in sources :

Example 41 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class TypeQueryBuilder method fromXContent.

public static TypeQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException {
    XContentParser parser = parseContext.parser();
    BytesRef type = null;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName)) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
                boost = parser.floatValue();
            } else if (VALUE_FIELD.match(currentFieldName)) {
                type = parser.utf8Bytes();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
        }
    }
    if (type == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter needs to be provided with a value for the type");
    }
    return new TypeQueryBuilder(type).boost(boost).queryName(queryName);
}
Also used : ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) BytesRef(org.apache.lucene.util.BytesRef)

Example 42 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class TribeService method buildClientSettings.

// pkg private for testing
/**
     * Builds node settings for a tribe client node from the tribe node's global settings,
     * combined with tribe specific settings.
     */
static Settings buildClientSettings(String tribeName, String parentNodeId, Settings globalSettings, Settings tribeSettings) {
    for (String tribeKey : tribeSettings.getAsMap().keySet()) {
        if (tribeKey.startsWith("path.")) {
            throw new IllegalArgumentException("Setting [" + tribeKey + "] not allowed in tribe client [" + tribeName + "]");
        }
    }
    Settings.Builder sb = Settings.builder().put(tribeSettings);
    sb.put(Node.NODE_NAME_SETTING.getKey(), Node.NODE_NAME_SETTING.get(globalSettings) + "/" + tribeName);
    // pass through ES home dir
    sb.put(Environment.PATH_HOME_SETTING.getKey(), Environment.PATH_HOME_SETTING.get(globalSettings));
    if (Environment.PATH_CONF_SETTING.exists(globalSettings)) {
        sb.put(Environment.PATH_CONF_SETTING.getKey(), Environment.PATH_CONF_SETTING.get(globalSettings));
    }
    if (Environment.PATH_LOGS_SETTING.exists(globalSettings)) {
        sb.put(Environment.PATH_LOGS_SETTING.getKey(), Environment.PATH_LOGS_SETTING.get(globalSettings));
    }
    if (Environment.PATH_SCRIPTS_SETTING.exists(globalSettings)) {
        sb.put(Environment.PATH_SCRIPTS_SETTING.getKey(), Environment.PATH_SCRIPTS_SETTING.get(globalSettings));
    }
    for (Setting<?> passthrough : PASS_THROUGH_SETTINGS) {
        if (passthrough.exists(tribeSettings) == false && passthrough.exists(globalSettings)) {
            sb.put(passthrough.getKey(), globalSettings.get(passthrough.getKey()));
        }
    }
    sb.put(TRIBE_NAME_SETTING.getKey(), tribeName);
    if (sb.get(NetworkModule.HTTP_ENABLED.getKey()) == null) {
        sb.put(NetworkModule.HTTP_ENABLED.getKey(), false);
    }
    sb.put(Node.NODE_DATA_SETTING.getKey(), false);
    sb.put(Node.NODE_MASTER_SETTING.getKey(), false);
    sb.put(Node.NODE_INGEST_SETTING.getKey(), false);
    // node id of a tribe client node is determined by node id of parent node and tribe name
    final BytesRef seedAsString = new BytesRef(parentNodeId + "/" + tribeName);
    long nodeIdSeed = MurmurHash3.hash128(seedAsString.bytes, seedAsString.offset, seedAsString.length, 0, new MurmurHash3.Hash128()).h1;
    sb.put(NodeEnvironment.NODE_ID_SEED_SETTING.getKey(), nodeIdSeed);
    sb.put(Node.NODE_LOCAL_STORAGE_SETTING.getKey(), false);
    return sb.build();
}
Also used : Settings(org.elasticsearch.common.settings.Settings) TransportSettings(org.elasticsearch.transport.TransportSettings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings) BytesRef(org.apache.lucene.util.BytesRef)

Example 43 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class CollapsingTopDocsCollectorTests method testCollapseString.

public void testCollapseString() throws Exception {
    CollapsingDocValuesProducer producer = new CollapsingDocValuesProducer<BytesRef>() {

        @Override
        public BytesRef randomGroup(int maxGroup) {
            return new BytesRef(Integer.toString(randomIntBetween(0, maxGroup - 1)));
        }

        @Override
        public void add(Document doc, BytesRef value, boolean multivalued) {
            if (multivalued) {
                doc.add(new SortedSetDocValuesField("field", value));
            } else {
                doc.add(new SortedDocValuesField("field", value));
            }
        }

        @Override
        public SortField sortField(boolean multivalued) {
            if (multivalued) {
                return new SortedSetSortField("field", false);
            } else {
                return new SortField("field", SortField.Type.STRING_VAL);
            }
        }
    };
    assertSearchCollapse(producer, false);
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) Document(org.apache.lucene.document.Document) BytesRef(org.apache.lucene.util.BytesRef)

Example 44 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class Correction method join.

public BytesRef join(BytesRef separator, BytesRefBuilder result, BytesRef preTag, BytesRef postTag) {
    BytesRef[] toJoin = new BytesRef[this.candidates.length];
    int len = separator.length * this.candidates.length - 1;
    for (int i = 0; i < toJoin.length; i++) {
        Candidate candidate = candidates[i];
        if (preTag == null || candidate.userInput) {
            toJoin[i] = candidate.term;
        } else {
            final int maxLen = preTag.length + postTag.length + candidate.term.length;
            // just allocate once
            final BytesRefBuilder highlighted = new BytesRefBuilder();
            highlighted.grow(maxLen);
            if (i == 0 || candidates[i - 1].userInput) {
                highlighted.append(preTag);
            }
            highlighted.append(candidate.term);
            if (toJoin.length == i + 1 || candidates[i + 1].userInput) {
                highlighted.append(postTag);
            }
            toJoin[i] = highlighted.get();
        }
        len += toJoin[i].length;
    }
    result.grow(len);
    return WordScorer.join(separator, result, toJoin);
}
Also used : Candidate(org.elasticsearch.search.suggest.phrase.DirectCandidateGenerator.Candidate) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 45 with BytesRef

use of org.apache.lucene.util.BytesRef in project elasticsearch by elastic.

the class DirectCandidateGenerator method drawCandidates.

@Override
public CandidateSet drawCandidates(CandidateSet set) throws IOException {
    Candidate original = set.originalTerm;
    BytesRef term = preFilter(original.term, spare, byteSpare);
    final long frequency = original.frequency;
    spellchecker.setThresholdFrequency(this.suggestMode == SuggestMode.SUGGEST_ALWAYS ? 0 : thresholdFrequency(frequency, dictSize));
    SuggestWord[] suggestSimilar = spellchecker.suggestSimilar(new Term(field, term), numCandidates, reader, this.suggestMode);
    List<Candidate> candidates = new ArrayList<>(suggestSimilar.length);
    for (int i = 0; i < suggestSimilar.length; i++) {
        SuggestWord suggestWord = suggestSimilar[i];
        BytesRef candidate = new BytesRef(suggestWord.string);
        postFilter(new Candidate(candidate, internalFrequency(candidate), suggestWord.score, score(suggestWord.freq, suggestWord.score, dictSize), false), spare, byteSpare, candidates);
    }
    set.addCandidates(candidates);
    return set;
}
Also used : SuggestWord(org.apache.lucene.search.spell.SuggestWord) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

BytesRef (org.apache.lucene.util.BytesRef)1449 Document (org.apache.lucene.document.Document)410 Directory (org.apache.lucene.store.Directory)370 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)266 ArrayList (java.util.ArrayList)186 Test (org.junit.Test)182 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)164 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)152 Term (org.apache.lucene.index.Term)124 Analyzer (org.apache.lucene.analysis.Analyzer)121 IndexReader (org.apache.lucene.index.IndexReader)121 TermsEnum (org.apache.lucene.index.TermsEnum)116 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)110 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)105 IOException (java.io.IOException)104 Field (org.apache.lucene.document.Field)101 StringField (org.apache.lucene.document.StringField)101 CrateUnitTest (io.crate.test.integration.CrateUnitTest)95 TextField (org.apache.lucene.document.TextField)95 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)87