Search in sources :

Example 1 with ParseContext

use of org.elasticsearch.index.mapper.ParseContext in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testExtractTerms.

public void testExtractTerms() throws Exception {
    addQueryMapping();
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("field", "term1"));
    bq.add(termQuery1, BooleanClause.Occur.SHOULD);
    TermQuery termQuery2 = new TermQuery(new Term("field", "term2"));
    bq.add(termQuery2, BooleanClause.Occur.SHOULD);
    DocumentMapper documentMapper = mapperService.documentMapper(typeName);
    PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
    ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(bq.build(), parseContext);
    ParseContext.Document document = parseContext.doc();
    PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType();
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE));
    List<IndexableField> fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name())));
    Collections.sort(fields, (field1, field2) -> field1.binaryValue().compareTo(field2.binaryValue()));
    assertThat(fields.size(), equalTo(2));
    assertThat(fields.get(0).binaryValue().utf8ToString(), equalTo("fieldterm1"));
    assertThat(fields.get(1).binaryValue().utf8ToString(), equalTo("fieldterm2"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) HasParentQueryBuilder(org.elasticsearch.index.query.HasParentQueryBuilder) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) RandomScoreFunctionBuilder(org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoostingQueryBuilder(org.elasticsearch.index.query.BoostingQueryBuilder) HasChildQueryBuilder(org.elasticsearch.index.query.HasChildQueryBuilder) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) IndexableField(org.apache.lucene.index.IndexableField) ParseContext(org.elasticsearch.index.mapper.ParseContext) QueryParseContext(org.elasticsearch.index.query.QueryParseContext)

Example 2 with ParseContext

use of org.elasticsearch.index.mapper.ParseContext in project elasticsearch by elastic.

the class PercolatorFieldMapper method processQuery.

void processQuery(Query query, ParseContext context) {
    ParseContext.Document doc = context.doc();
    FieldType pft = (FieldType) this.fieldType();
    QueryAnalyzer.Result result;
    try {
        result = QueryAnalyzer.analyze(query);
    } catch (QueryAnalyzer.UnsupportedQueryException e) {
        doc.add(new Field(pft.extractionResultField.name(), EXTRACTION_FAILED, extractionResultField.fieldType()));
        return;
    }
    for (Term term : result.terms) {
        BytesRefBuilder builder = new BytesRefBuilder();
        builder.append(new BytesRef(term.field()));
        builder.append(FIELD_VALUE_SEPARATOR);
        builder.append(term.bytes());
        doc.add(new Field(queryTermsField.name(), builder.toBytesRef(), queryTermsField.fieldType()));
    }
    if (result.verified) {
        doc.add(new Field(extractionResultField.name(), EXTRACTION_COMPLETE, extractionResultField.fieldType()));
    } else {
        doc.add(new Field(extractionResultField.name(), EXTRACTION_PARTIAL, extractionResultField.fieldType()));
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) ParseContext(org.elasticsearch.index.mapper.ParseContext) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType)

Example 3 with ParseContext

use of org.elasticsearch.index.mapper.ParseContext in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testExtractTermsAndRanges_failed.

public void testExtractTermsAndRanges_failed() throws Exception {
    addQueryMapping();
    TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true);
    DocumentMapper documentMapper = mapperService.documentMapper(typeName);
    PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
    ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(query, parseContext);
    ParseContext.Document document = parseContext.doc();
    PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType();
    assertThat(document.getFields().size(), equalTo(1));
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_FAILED));
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ParseContext(org.elasticsearch.index.mapper.ParseContext) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with ParseContext

use of org.elasticsearch.index.mapper.ParseContext in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testExtractTermsAndRanges_partial.

public void testExtractTermsAndRanges_partial() throws Exception {
    addQueryMapping();
    PhraseQuery phraseQuery = new PhraseQuery("field", "term");
    DocumentMapper documentMapper = mapperService.documentMapper(typeName);
    PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
    ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(phraseQuery, parseContext);
    ParseContext.Document document = parseContext.doc();
    PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType();
    assertThat(document.getFields().size(), equalTo(2));
    assertThat(document.getFields().get(0).binaryValue().utf8ToString(), equalTo("fieldterm"));
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_PARTIAL));
}
Also used : PhraseQuery(org.apache.lucene.search.PhraseQuery) QueryBuilders.matchPhraseQuery(org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ParseContext(org.elasticsearch.index.mapper.ParseContext) QueryParseContext(org.elasticsearch.index.query.QueryParseContext)

Aggregations

ParseContext (org.elasticsearch.index.mapper.ParseContext)4 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)4 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)3 IndexableField (org.apache.lucene.index.IndexableField)2 Term (org.apache.lucene.index.Term)2 BytesRef (org.apache.lucene.util.BytesRef)2 ArrayList (java.util.ArrayList)1 Field (org.apache.lucene.document.Field)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 XContentFactory.jsonBuilder (org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 BoostingQueryBuilder (org.elasticsearch.index.query.BoostingQueryBuilder)1 ConstantScoreQueryBuilder (org.elasticsearch.index.query.ConstantScoreQueryBuilder)1 HasChildQueryBuilder (org.elasticsearch.index.query.HasChildQueryBuilder)1