List of usage examples for org.apache.lucene.search MatchNoDocsQuery MatchNoDocsQuery
public MatchNoDocsQuery(String reason)
From source file:org.elasticsearch.index.search.QueryStringQueryParser.java
License:Apache License
private Query getPossiblyAnalyzedPrefixQuery(String field, String termStr) throws ParseException { if (analyzeWildcard == false) { return super.getPrefixQuery(field, termStr); }/* ww w .j a v a2 s. com*/ List<List<String>> tlist; // get Analyzer from superclass and tokenize the term TokenStream source = null; try { try { source = getAnalyzer().tokenStream(field, termStr); source.reset(); } catch (IOException e) { return super.getPrefixQuery(field, termStr); } tlist = new ArrayList<>(); List<String> currentPos = new ArrayList<>(); CharTermAttribute termAtt = source.addAttribute(CharTermAttribute.class); PositionIncrementAttribute posAtt = source.addAttribute(PositionIncrementAttribute.class); while (true) { try { if (!source.incrementToken()) break; } catch (IOException e) { break; } if (currentPos.isEmpty() == false && posAtt.getPositionIncrement() > 0) { tlist.add(currentPos); currentPos = new ArrayList<>(); } currentPos.add(termAtt.toString()); } if (currentPos.isEmpty() == false) { tlist.add(currentPos); } } finally { if (source != null) { IOUtils.closeWhileHandlingException(source); } } if (tlist.size() == 0) { return new MatchNoDocsQuery("analysis was empty for " + field + ":" + termStr); } if (tlist.size() == 1 && tlist.get(0).size() == 1) { return super.getPrefixQuery(field, tlist.get(0).get(0)); } // build a boolean query with prefix on the last position only. List<BooleanClause> clauses = new ArrayList<>(); for (int pos = 0; pos < tlist.size(); pos++) { List<String> plist = tlist.get(pos); boolean isLastPos = (pos == tlist.size() - 1); Query posQuery; if (plist.size() == 1) { if (isLastPos) { posQuery = super.getPrefixQuery(field, plist.get(0)); } else { posQuery = newTermQuery(new Term(field, plist.get(0))); } } else if (isLastPos == false) { // build a synonym query for terms in the same position. Term[] terms = new Term[plist.size()]; for (int i = 0; i < plist.size(); i++) { terms[i] = new Term(field, plist.get(i)); } posQuery = new SynonymQuery(terms); } else { List<BooleanClause> innerClauses = new ArrayList<>(); for (String token : plist) { innerClauses .add(new BooleanClause(super.getPrefixQuery(field, token), BooleanClause.Occur.SHOULD)); } posQuery = getBooleanQuery(innerClauses); } clauses.add(new BooleanClause(posQuery, getDefaultOperator() == Operator.AND ? BooleanClause.Occur.MUST : BooleanClause.Occur.SHOULD)); } return getBooleanQuery(clauses); }
From source file:org.elasticsearch.index.search.QueryStringQueryParser.java
License:Apache License
private Query existsQuery(String fieldName) { final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType) context .getMapperService().fullName(FieldNamesFieldMapper.NAME); if (fieldNamesFieldType == null) { return new MatchNoDocsQuery("No mappings yet"); }/*from w w w. ja v a 2 s . c o m*/ if (fieldNamesFieldType.isEnabled() == false) { // The field_names_field is disabled so we switch to a wildcard query that matches all terms return new WildcardQuery(new Term(fieldName, "*")); } return ExistsQueryBuilder.newFilter(context, fieldName); }
From source file:org.elasticsearch.search.slice.SliceBuilder.java
License:Apache License
public Query toFilter(QueryShardContext context, int shardId, int numShards) { final MappedFieldType type = context.fieldMapper(field); if (type == null) { throw new IllegalArgumentException("field " + field + " not found"); }/*from w w w .j a va2s . c o m*/ boolean useTermQuery = false; if (UidFieldMapper.NAME.equals(field)) { useTermQuery = true; } else if (type.hasDocValues() == false) { throw new IllegalArgumentException("cannot load numeric doc values on " + field); } else { IndexFieldData ifm = context.getForField(type); if (ifm instanceof IndexNumericFieldData == false) { throw new IllegalArgumentException("cannot load numeric doc values on " + field); } } if (numShards == 1) { return useTermQuery ? new TermsSliceQuery(field, id, max) : new DocValuesSliceQuery(field, id, max); } if (max >= numShards) { // the number of slices is greater than the number of shards // in such case we can reduce the number of requested shards by slice // first we check if the slice is responsible of this shard int targetShard = id % numShards; if (targetShard != shardId) { // the shard is not part of this slice, we can skip it. return new MatchNoDocsQuery("this shard is not part of the slice"); } // compute the number of slices where this shard appears int numSlicesInShard = max / numShards; int rest = max % numShards; if (rest > targetShard) { numSlicesInShard++; } if (numSlicesInShard == 1) { // this shard has only one slice so we must check all the documents return new MatchAllDocsQuery(); } // get the new slice id for this shard int shardSlice = id / numShards; return useTermQuery ? new TermsSliceQuery(field, shardSlice, numSlicesInShard) : new DocValuesSliceQuery(field, shardSlice, numSlicesInShard); } // the number of shards is greater than the number of slices // check if the shard is assigned to the slice int targetSlice = shardId % max; if (id != targetSlice) { // the shard is not part of this slice, we can skip it. return new MatchNoDocsQuery("this shard is not part of the slice"); } return new MatchAllDocsQuery(); }