Java Code Examples for org.apache.solr.schema.FieldType#readableToIndexed()

The following examples show how to use org.apache.solr.schema.FieldType#readableToIndexed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroupConverter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
static Collection<SearchGroup<BytesRef>> fromMutable(SchemaField field, Collection<SearchGroup<MutableValue>> values) {
  if (values == null) {
    return null;
  }
  FieldType fieldType = field.getType();
  List<SearchGroup<BytesRef>> result = new ArrayList<>(values.size());
  for (SearchGroup<MutableValue> original : values) {
    SearchGroup<BytesRef> converted = new SearchGroup<>();
    converted.sortValues = original.sortValues;
    if (original.groupValue.exists) {
      BytesRefBuilder binary = new BytesRefBuilder();
      fieldType.readableToIndexed(Utils.OBJECT_TO_STRING.apply(original.groupValue.toObject()), binary);
      converted.groupValue = binary.get();
    } else {
      converted.groupValue = null;
    }
    result.add(converted);
  }
  return result;
}
 
Example 2
Source File: GroupConverter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
static TopGroups<BytesRef> fromMutable(SchemaField field, TopGroups<MutableValue> values) {
  if (values == null) {
    return null;
  }
  
  FieldType fieldType = field.getType();

  GroupDocs<BytesRef> groupDocs[] = new GroupDocs[values.groups.length];

  for (int i = 0; i < values.groups.length; i++) {
    GroupDocs<MutableValue> original = values.groups[i];
    final BytesRef groupValue;
    if (original.groupValue.exists) {
      BytesRefBuilder binary = new BytesRefBuilder();
      fieldType.readableToIndexed(Utils.OBJECT_TO_STRING.apply(original.groupValue.toObject()), binary);
      groupValue = binary.get();
    } else {
      groupValue = null;
    }
    groupDocs[i] = new GroupDocs<>(original.score, original.maxScore, original.totalHits, original.scoreDocs, groupValue, original.groupSortValues);
  }
  
  return new TopGroups<>(values.groupSort, values.withinGroupSort, values.totalHitCount, values.totalGroupedHitCount, groupDocs, values.maxScore);
}
 
Example 3
Source File: ExpandComponent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Query getGroupQuery(String fname,
                         FieldType ft,
                         int size,
                         LongHashSet groupSet) {

  BytesRef[] bytesRefs = new BytesRef[size];
  int index = -1;
  BytesRefBuilder term = new BytesRefBuilder();
  Iterator<LongCursor> it = groupSet.iterator();

  while (it.hasNext()) {
    LongCursor cursor = it.next();
    String stringVal = numericToString(ft, cursor.value);
    ft.readableToIndexed(stringVal, term);
    bytesRefs[++index] = term.toBytesRef();
  }

  return new TermInSetQuery(fname, bytesRefs);
}
 
Example 4
Source File: XJoinQParserPlugin.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
static private Transformer transformer(final FieldType ft) {
  return new Transformer() {
    
    BytesRefBuilder term = new BytesRefBuilder();
    
    @Override
    public BytesRef transform(Object joinId) {
      String joinStr = joinId.toString();
      // logic same as TermQParserPlugin
      if (ft != null) {
        ft.readableToIndexed(joinStr, term);
      } else {
        term.copyChars(joinStr);
      }
      return term.toBytesRef();
    }
    
  };
}
 
Example 5
Source File: XJoinQParserPlugin.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
static private Transformer transformer(final FieldType ft) {
  return new Transformer() {
    
    BytesRefBuilder term = new BytesRefBuilder();
    
    @Override
    public BytesRef transform(Object joinId) {
      String joinStr = joinId.toString();
      // logic same as TermQParserPlugin
      if (ft != null) {
        ft.readableToIndexed(joinStr, term);
      } else {
        term.copyChars(joinStr);
      }
      return term.toBytesRef();
    }
    
  };
}
 
Example 6
Source File: XJoinQParserPlugin.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
static private Transformer transformer(final FieldType ft) {
  return new Transformer() {
    
    BytesRef term = new BytesRef();
    
    @Override
    public BytesRef transform(Object joinId) {
      if (joinId == null) {
        throw new RuntimeException("joinId is null! (weird)");
      }
      String joinStr = joinId.toString();
      // logic same as TermQParserPlugin
      if (ft != null) {
        ft.readableToIndexed(joinStr, term);
      } else {
        term.copyChars(joinStr);
      }
      return BytesRef.deepCopyOf(term);
    }
    
  };
}
 
Example 7
Source File: SecureRealTimeGetComponent.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
/**
 * @param doc SolrDocument to check
 * @param idField field where the id is stored
 * @param fieldType type of id field
 * @param filterQuery Query to filter by
 * @param searcher SolrIndexSearcher on which to apply the filter query
 * @returns the internal docid, or -1 if doc is not found or doesn't match filter
 */
private static int getFilteredInternalDocId(SolrDocument doc, SchemaField idField, FieldType fieldType,
      Query filterQuery, SolrIndexSearcher searcher) throws IOException {
  int docid = -1;
  Field f = (Field)doc.getFieldValue(idField.getName());
  String idStr = f.stringValue();
  BytesRef idBytes = new BytesRef();
  fieldType.readableToIndexed(idStr, idBytes);
  // get the internal document id
  long segAndId = searcher.lookupId(idBytes);

    // if docid is valid, run it through the filter
  if (segAndId >= 0) {
    int segid = (int) segAndId;
    AtomicReaderContext ctx = searcher.getTopReaderContext().leaves().get((int) (segAndId >> 32));
    docid = segid + ctx.docBase;
    Weight weight = filterQuery.createWeight(searcher);
    Scorer scorer = weight.scorer(ctx, null);
    if (scorer == null || segid != scorer.advance(segid)) {
      // filter doesn't match.
      docid = -1;
    }
  }
  return docid;
}
 
Example 8
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean isInIndex(long id, LRU cache, String fieldName, boolean populateCache, SolrCore core) throws IOException
{
    if(cache.containsKey(id))
    {
        return true;
    }
    else
    {
        RefCounted<SolrIndexSearcher> refCounted = null;
        try
        {
            if(populateCache)
            {
                cache.put(id, null); // Safe to add this here because we reset this on rollback.
            }
            refCounted = core.getSearcher();
            SolrIndexSearcher searcher = refCounted.get();
            FieldType fieldType = searcher.getSchema().getField(fieldName).getType();
            TermQuery q = new TermQuery(new Term(fieldName, fieldType.readableToIndexed(Long.toString(id))));
            TopDocs topDocs = searcher.search(q, 1);
            return topDocs.totalHits > 0;
        }
        finally
        {
            ofNullable(refCounted).ifPresent(RefCounted::decref);
        }
    }
}
 
Example 9
Source File: DocBasedVersionConstraintsProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Inspects a raw field value (which may come from a doc in the index, or a
 * doc in the UpdateLog that still has String values, or a String sent by
 * the user as a param) and if it is a String, asks the versionField FieldType
 * to convert it to an Object suitable for comparison.
 */
private static Object convertFieldValueUsingType(final Object rawValue, SchemaField field) {
  if (rawValue instanceof CharSequence) {
    // in theory, the FieldType might still be CharSequence based,
    // but in that case trust it to do an identity conversion...
    FieldType fieldType = field.getType();
    BytesRefBuilder term = new BytesRefBuilder();
    fieldType.readableToIndexed((CharSequence)rawValue, term);
    return fieldType.toObject(field, term.get());
  }
  // else...
  return rawValue;
}
 
Example 10
Source File: SolrInformationServer.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public List<NodeMetaData> getCascadeNodes(List<Long> txnIds) throws IOException, JSONException
{
    List<FieldInstance> list = dataModel.getIndexedFieldNamesForProperty(ContentModel.PROP_CASCADE_TX).getFields();
    FieldInstance fieldInstance = list.get(0);

    RefCounted<SolrIndexSearcher> refCounted = null;
    IntArrayList docList;
    Set<Long> parentNodesId = new HashSet<>();

    try
    {
        refCounted = core.getSearcher();
        SolrIndexSearcher searcher = refCounted.get();
        String field = fieldInstance.getField();
        SchemaField schemaField = searcher.getSchema().getField(field);
        FieldType fieldType = schemaField.getType();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery booleanQuery;

        for(Long l : txnIds)
        {
            BytesRefBuilder bytesRefBuilder = new BytesRefBuilder();
            fieldType.readableToIndexed(l.toString(), bytesRefBuilder);
            TermQuery termQuery = new TermQuery(new Term(field, bytesRefBuilder.toBytesRef()));
            BooleanClause booleanClause = new BooleanClause(termQuery, BooleanClause.Occur.SHOULD);
            builder.add(booleanClause);
        }

        booleanQuery = builder.build();

        DocListCollector collector = new DocListCollector();
        searcher.search(booleanQuery, collector);
        docList = collector.getDocs();
        int size = docList.size();
        for(int i=0; i<size; i++)
        {
            int docId = docList.get(i);
            Document document = searcher.doc(docId, REQUEST_ONLY_ID_FIELD);
            IndexableField indexableField = document.getField(FIELD_SOLR4_ID);
            String id = indexableField.stringValue();
            TenantDbId ids = AlfrescoSolrDataModel.decodeNodeDocumentId(id);
            parentNodesId.add(ids.dbId);
        }
    }
    finally
    {
        ofNullable(refCounted).ifPresent(RefCounted::decref);
    }

    List<NodeMetaData> allNodeMetaDatas = new ArrayList<>();

    for (Long parentNodeId : parentNodesId)
    {
        NodeMetaDataParameters nmdp = new NodeMetaDataParameters();
        nmdp.setFromNodeId(parentNodeId);
        nmdp.setToNodeId(parentNodeId);
        nmdp.setIncludeAclId(true);
        nmdp.setIncludeChildAssociations(false);
        nmdp.setIncludeChildIds(true);
        nmdp.setIncludeOwner(false);
        nmdp.setIncludeParentAssociations(false);
        nmdp.setIncludePaths(true);
        nmdp.setIncludeProperties(false);
        nmdp.setIncludeTxnId(true);
        nmdp.setMaxResults(1);
        // Gets only one
        Optional<Collection<NodeMetaData>> nodeMetaDatas = getNodesMetaDataFromRepository(nmdp);
        allNodeMetaDatas.addAll(nodeMetaDatas.orElse(Collections.emptyList()));
    }

    return allNodeMetaDatas;
}
 
Example 11
Source File: PivotFacetProcessor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Recursive function to compute all the pivot counts for the values under the specified field
 */
protected List<NamedList<Object>> doPivots(NamedList<Integer> superFacets,
                                           String field, String subField,
                                           Deque<String> fnames, Deque<String> vnames,
                                           ParsedParams parsed, List<StatsField> statsFields,
                                           List<FacetComponent.FacetBase> facetQueries, List<RangeFacetRequest> facetRanges)
    throws IOException {

  boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false);

  SolrIndexSearcher searcher = rb.req.getSearcher();
  // TODO: optimize to avoid converting to an external string and then having to convert back to internal below
  SchemaField sfield = searcher.getSchema().getField(field);
  FieldType ftype = sfield.getType();

  String nextField = fnames.poll();

  // re-usable BytesRefBuilder for conversion of term values to Objects
  BytesRefBuilder termval = new BytesRefBuilder(); 

  List<NamedList<Object>> values = new ArrayList<>( superFacets.size() );
  for (Map.Entry<String, Integer> kv : superFacets) {
    // Only sub-facet if parent facet has positive count - still may not be any values for the sub-field though
    if (kv.getValue() >= getMinCountForField(field)) {  
      final String fieldValue = kv.getKey();
      final int pivotCount = kv.getValue();

      SimpleOrderedMap<Object> pivot = new SimpleOrderedMap<>();
      pivot.add( "field", field );
      if (null == fieldValue) {
        pivot.add( "value", null );
      } else {
        ftype.readableToIndexed(fieldValue, termval);
        pivot.add( "value", ftype.toObject(sfield, termval.get()) );
      }
      pivot.add( "count", pivotCount );

      final DocSet subset = getSubset(parsed.docs, sfield, fieldValue);
      
      addPivotQueriesAndRanges(pivot, params, subset, facetQueries, facetRanges);

      if( subField != null )  {
        NamedList<Integer> facetCounts;
        if(!vnames.isEmpty()){
          String val = vnames.pop();
          facetCounts = new NamedList<>();
          facetCounts.add(val, getSubsetSize(subset,
                                             searcher.getSchema().getField(subField),
                                             val));
        } else {
          facetCounts = this.getTermCountsForPivots(subField, parsed.withDocs(subset));
        }

        if (facetCounts.size() >= 1) {
          pivot.add( "pivot", doPivots( facetCounts, subField, nextField, fnames, vnames, parsed.withDocs(subset), statsFields, facetQueries, facetRanges) );
        }
      }
      if ((isShard || 0 < pivotCount) && ! statsFields.isEmpty()) {
        Map<String, StatsValues> stv = new LinkedHashMap<>();
        for (StatsField statsField : statsFields) {
          stv.put(statsField.getOutputKey(), statsField.computeLocalStatsValues(subset));
        }
        pivot.add("stats", StatsComponent.convertToResponse(stv));
      }
      values.add( pivot );
    }

  }
  // put the field back on the list
  fnames.push( nextField );
  return values;
}
 
Example 12
Source File: TermsComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void fetchTerms(SolrIndexSearcher indexSearcher, String[] fields, String termList,
    boolean includeTotalTermFreq, NamedList<Object> result) throws IOException {
  List<String> splitTermList = StrUtils.splitSmart(termList, ",", true);
  // Sort the terms once
  String[] splitTerms = splitTermList.toArray(new String[splitTermList.size()]);
  // Not a great idea to trim here, but it was in the original implementation
  for (int i = 0; i < splitTerms.length; i++) {
    splitTerms[i] = splitTerms[i].trim();
  }
  Arrays.sort(splitTerms);

  IndexReaderContext topReaderContext = indexSearcher.getTopReaderContext();
  for (String field : fields) {
    SchemaField sf = indexSearcher.getSchema().getField(field);
    FieldType fieldType = sf.getType();
    NamedList<Object> termsMap = new NamedList<>();

    if (fieldType.isPointField()) {
      for (String term : splitTerms) {
        Query q = fieldType.getFieldQuery(null, sf, term);
        int count = indexSearcher.getDocSet(q).size();
        termsMap.add(term, count);
      }
    } else {

      // Since splitTerms is already sorted, this array will also be sorted. NOTE: this may not be true, it depends on readableToIndexed.
      Term[] terms = new Term[splitTerms.length];
      for (int i = 0; i < splitTerms.length; i++) {
        terms[i] = new Term(field, fieldType.readableToIndexed(splitTerms[i]));
      }

      TermStates[] termStates = new TermStates[terms.length];
      collectTermStates(topReaderContext, termStates, terms);

      for (int i = 0; i < terms.length; i++) {
        if (termStates[i] != null) {
          String outTerm = fieldType.indexedToReadable(terms[i].bytes().utf8ToString());
          int docFreq = termStates[i].docFreq();
          addTermToNamedList(termsMap, outTerm, docFreq, termStates[i].totalTermFreq(), includeTotalTermFreq);
        }
      }
    }

    result.add(field, termsMap);
  }
}