|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.mapper; |
| 10 | + |
| 11 | +import org.apache.lucene.analysis.TokenStream; |
| 12 | +import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; |
| 13 | +import org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute; |
| 14 | +import org.apache.lucene.document.FieldType; |
| 15 | +import org.apache.lucene.index.IndexOptions; |
| 16 | +import org.apache.lucene.index.Term; |
| 17 | +import org.apache.lucene.search.BooleanClause; |
| 18 | +import org.apache.lucene.search.BooleanQuery; |
| 19 | +import org.apache.lucene.search.MultiPhraseQuery; |
| 20 | +import org.apache.lucene.search.PhraseQuery; |
| 21 | +import org.apache.lucene.search.Query; |
| 22 | +import org.apache.lucene.search.TermQuery; |
| 23 | +import org.opensearch.Version; |
| 24 | +import org.opensearch.common.lucene.search.MultiPhrasePrefixQuery; |
| 25 | +import org.opensearch.index.analysis.IndexAnalyzers; |
| 26 | +import org.opensearch.index.query.QueryShardContext; |
| 27 | +import org.opensearch.index.query.SourceFieldMatchQuery; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class MatchOnlyTextFieldMapper extends TextFieldMapper { |
| 34 | + |
| 35 | + public static final FieldType FIELD_TYPE = new FieldType(); |
| 36 | + public static final String CONTENT_TYPE = "match_only_text"; |
| 37 | + |
| 38 | + @Override |
| 39 | + protected String contentType() { |
| 40 | + return CONTENT_TYPE; |
| 41 | + } |
| 42 | + |
| 43 | + static { |
| 44 | + FIELD_TYPE.setTokenized(true); |
| 45 | + FIELD_TYPE.setStored(false); |
| 46 | + FIELD_TYPE.setStoreTermVectors(false); |
| 47 | + FIELD_TYPE.setOmitNorms(true); |
| 48 | + FIELD_TYPE.setIndexOptions(IndexOptions.DOCS); |
| 49 | + FIELD_TYPE.freeze(); |
| 50 | + } |
| 51 | + |
| 52 | + public static final TypeParser PARSER = new TypeParser((n, c) -> new Builder(n, c.indexVersionCreated(), c.getIndexAnalyzers())); |
| 53 | + |
| 54 | + protected MatchOnlyTextFieldMapper(String simpleName, FieldType fieldType, MatchOnlyTextFieldType mappedFieldType, |
| 55 | + TextFieldMapper.PrefixFieldMapper prefixFieldMapper, |
| 56 | + TextFieldMapper.PhraseFieldMapper phraseFieldMapper, |
| 57 | + MultiFields multiFields, CopyTo copyTo, Builder builder) { |
| 58 | + |
| 59 | + super(simpleName, fieldType, mappedFieldType, prefixFieldMapper, phraseFieldMapper, multiFields, copyTo, builder); |
| 60 | + } |
| 61 | + |
| 62 | + public static class Builder extends TextFieldMapper.Builder { |
| 63 | + |
| 64 | + public Builder(String name, IndexAnalyzers indexAnalyzers) { |
| 65 | + super(name, indexAnalyzers); |
| 66 | + } |
| 67 | + |
| 68 | + public Builder(String name, Version indexCreatedVersion, IndexAnalyzers indexAnalyzers) { |
| 69 | + super(name, indexCreatedVersion, indexAnalyzers); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public MatchOnlyTextFieldMapper build(BuilderContext context) { |
| 74 | + FieldType fieldType = FIELD_TYPE; |
| 75 | + MatchOnlyTextFieldType tft = new MatchOnlyTextFieldType(buildFieldType(fieldType, context)); |
| 76 | + return new MatchOnlyTextFieldMapper( |
| 77 | + name, |
| 78 | + fieldType, |
| 79 | + tft, |
| 80 | + buildPrefixMapper(context, fieldType, tft), |
| 81 | + buildPhraseMapper(fieldType, tft), |
| 82 | + multiFieldsBuilder.build(this, context), |
| 83 | + copyTo.build(), |
| 84 | + this |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static final class MatchOnlyTextFieldType extends TextFieldMapper.TextFieldType { |
| 90 | + |
| 91 | + @Override |
| 92 | + public String typeName() { |
| 93 | + return CONTENT_TYPE; |
| 94 | + } |
| 95 | + |
| 96 | + public MatchOnlyTextFieldType(TextFieldMapper.TextFieldType tft) { |
| 97 | + super(tft.name(), tft.isSearchable(), tft.isStored(), tft.getTextSearchInfo(), tft.meta()); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Query phraseQuery(TokenStream stream, int slop, boolean enablePosIncrements, QueryShardContext context) throws IOException { |
| 102 | + PhraseQuery phraseQuery = (PhraseQuery) super.phraseQuery(stream, slop, enablePosIncrements); |
| 103 | + BooleanQuery.Builder builder = new BooleanQuery.Builder(); |
| 104 | + for (Term term: phraseQuery.getTerms()) { |
| 105 | + builder.add(new TermQuery(term), BooleanClause.Occur.FILTER); |
| 106 | + } |
| 107 | + return new SourceFieldMatchQuery(builder.build(), phraseQuery, this, |
| 108 | + (SourceValueFetcher) this.valueFetcher(context, context.lookup(), null), context.lookup()); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Query multiPhraseQuery(TokenStream stream, int slop, boolean enablePositionIncrements, QueryShardContext context) throws IOException { |
| 113 | + MultiPhraseQuery multiPhraseQuery = (MultiPhraseQuery) super.multiPhraseQuery(stream, slop, enablePositionIncrements); |
| 114 | + BooleanQuery.Builder builder = new BooleanQuery.Builder(); |
| 115 | + for (Term[] terms : multiPhraseQuery.getTermArrays()) { |
| 116 | + BooleanQuery.Builder disjunctions = new BooleanQuery.Builder(); |
| 117 | + for (Term term: terms) { |
| 118 | + disjunctions.add(new TermQuery(term), BooleanClause.Occur.SHOULD); |
| 119 | + } |
| 120 | + builder.add(disjunctions.build(), BooleanClause.Occur.FILTER); |
| 121 | + } |
| 122 | + return new SourceFieldMatchQuery(builder.build(), multiPhraseQuery, this, |
| 123 | + (SourceValueFetcher) this.valueFetcher(context, context.lookup(), null), context.lookup()); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Query phrasePrefixQuery(TokenStream stream, int slop, int maxExpansions, QueryShardContext context) throws IOException { |
| 128 | + Query phrasePrefixQuery = super.phrasePrefixQuery(stream, slop, maxExpansions); |
| 129 | + List<List<Term>> termArray = getTermsFromTokenStream(stream); |
| 130 | + BooleanQuery.Builder builder = new BooleanQuery.Builder(); |
| 131 | + for (int i = 0; i < termArray.size(); i++) { |
| 132 | + BooleanQuery.Builder disjunctions = new BooleanQuery.Builder(); |
| 133 | + for (Term term: termArray.get(i)) { |
| 134 | + if (i == termArray.size() - 1) { |
| 135 | + MultiPhrasePrefixQuery mqb = new MultiPhrasePrefixQuery(name()); |
| 136 | + mqb.add(term); |
| 137 | + disjunctions.add(mqb, BooleanClause.Occur.SHOULD); |
| 138 | + } else { |
| 139 | + disjunctions.add(new TermQuery(term), BooleanClause.Occur.SHOULD); |
| 140 | + } |
| 141 | + } |
| 142 | + builder.add(disjunctions.build(), BooleanClause.Occur.FILTER); |
| 143 | + } |
| 144 | + return new SourceFieldMatchQuery(builder.build(), phrasePrefixQuery, this, |
| 145 | + (SourceValueFetcher) this.valueFetcher(context, context.lookup(), null), context.lookup()); |
| 146 | + } |
| 147 | + |
| 148 | + private List<List<Term>> getTermsFromTokenStream(TokenStream stream) throws IOException { |
| 149 | + final List<List<Term>> termArray = new ArrayList<>(); |
| 150 | + TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class); |
| 151 | + PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class); |
| 152 | + List<Term> currentTerms = new ArrayList<>(); |
| 153 | + stream.reset(); |
| 154 | + while (stream.incrementToken()) { |
| 155 | + if (posIncrAtt.getPositionIncrement() != 0) { |
| 156 | + if (currentTerms.isEmpty() == false) { |
| 157 | + termArray.add(List.copyOf(currentTerms)); |
| 158 | + } |
| 159 | + currentTerms.clear(); |
| 160 | + } |
| 161 | + currentTerms.add(new Term(name(), termAtt.getBytesRef())); |
| 162 | + } |
| 163 | + termArray.add(List.copyOf(currentTerms)); |
| 164 | + return termArray; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments