public EdgeCountScoringStrategyConfiguration(GraphConfiguration config) { useEdgeBoost = getUseEdgeBoost(config); updateEdgeBoost = getUpdateEdgeBoost(config); inEdgeBoost = getInEdgeBoost(config); outEdgeBoost = getOutEdgeBoost(config); scoreFormula = getScoreFormula(config); }
public EdgeCountScoringStrategy(GraphConfiguration config) { super(config); this.config = new EdgeCountScoringStrategyConfiguration(config); }
public boolean isUpdateEdgeBoost() { return isUseEdgeBoost() && updateEdgeBoost; }
@Override public QueryBuilder updateQuery(QueryBuilder query) { if (!getConfig().isUseEdgeBoost()) { return query; } ScoreFunctionBuilder scoreFunction = ScoreFunctionBuilders .scriptFunction(getConfig().getScoreFormula(), "groovy") .param("inEdgeMultiplier", getConfig().getInEdgeBoost()) .param("outEdgeMultiplier", getConfig().getOutEdgeBoost()); return QueryBuilders.functionScoreQuery(query, scoreFunction); }
@Override public void addElement(SearchIndex searchIndex, Graph graph, Element element, Authorizations authorizations) { if (!getConfig().isUpdateEdgeBoost()) { return; } if (!(element instanceof Edge)) { return; } Element vOut = ((Edge) element).getVertex(Direction.OUT, authorizations); if (vOut != null) { searchIndex.addElement(graph, vOut, authorizations); } Element vIn = ((Edge) element).getVertex(Direction.IN, authorizations); if (vIn != null) { searchIndex.addElement(graph, vIn, authorizations); } }
@Override public List<String> getFieldNames() { List<String> fieldNames = super.getFieldNames(); fieldNames.add(EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME); fieldNames.add(EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME); return fieldNames; }
protected void createIndexAddFieldsToElementType(XContentBuilder builder) throws IOException { builder .startObject(ELEMENT_TYPE_FIELD_NAME).field("type", "string").field("store", "true").endObject() .startObject(VISIBILITY_FIELD_NAME).field("type", "string").field("analyzer", "keyword").field("index", "not_analyzed").field("store", "true").endObject() ; getConfig().getScoringStrategy().addFieldsToElementType(builder); }
private SearchResponse getSearchResponse(String elementType) { List<FilterBuilder> filters = getFilters(elementType); QueryBuilder query = createQuery(getParameters().getQueryString(), elementType, filters); query = scoringStrategy.updateQuery(query); SearchRequestBuilder q = getSearchRequestBuilder(filters, query); LOGGER.debug("query: " + q); return q.execute() .actionGet(); }
public EdgeCountScoringStrategyConfiguration(GraphConfiguration config) { useEdgeBoost = getUseEdgeBoost(config); updateEdgeBoost = getUpdateEdgeBoost(config); inEdgeBoost = getInEdgeBoost(config); outEdgeBoost = getOutEdgeBoost(config); scoreFormula = getScoreFormula(config); }
@Override public QueryBuilder updateQuery(QueryBuilder query) { if (!getConfig().isUseEdgeBoost()) { return query; } ScoreFunctionBuilder scoreFunction = ScoreFunctionBuilders .scriptFunction(getConfig().getScoreFormula(), "groovy") .param("inEdgeMultiplier", getConfig().getInEdgeBoost()) .param("outEdgeMultiplier", getConfig().getOutEdgeBoost()); return QueryBuilders.functionScoreQuery(query, scoreFunction); }
@Override public void addElement(SearchIndex searchIndex, Graph graph, Element element, Authorizations authorizations) { if (!getConfig().isUpdateEdgeBoost()) { return; } if (!(element instanceof Edge)) { return; } Element vOut = ((Edge) element).getVertex(Direction.OUT, authorizations); if (vOut != null) { searchIndex.addElement(graph, vOut, authorizations); } Element vIn = ((Edge) element).getVertex(Direction.IN, authorizations); if (vIn != null) { searchIndex.addElement(graph, vIn, authorizations); } }
@Override public List<String> getFieldNames() { List<String> fieldNames = super.getFieldNames(); fieldNames.add(EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME); fieldNames.add(EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME); return fieldNames; }
public EdgeCountScoringStrategy(GraphConfiguration config) { super(config); this.config = new EdgeCountScoringStrategyConfiguration(config); }
public boolean isUpdateEdgeBoost() { return isUseEdgeBoost() && updateEdgeBoost; }
protected void createIndexAddFieldsToElementType(XContentBuilder builder) throws IOException { builder .startObject(ELEMENT_TYPE_FIELD_NAME).field("type", "string").field("store", "true").endObject() .startObject(VISIBILITY_FIELD_NAME).field("type", "string").field("analyzer", "keyword").field("index", "not_analyzed").field("store", "true").endObject() ; getConfig().getScoringStrategy().addFieldsToElementType(builder); }
@Override public boolean addFieldsToVertexDocument(SearchIndex searchIndex, XContentBuilder jsonBuilder, Vertex vertex, GetResponse existingDocument, Authorizations authorizations) throws IOException { if (existingDocument != null && !getConfig().isUpdateEdgeBoost()) { return false; } boolean changed = false; int inEdgeCount = vertex.getEdgeCount(Direction.IN, authorizations); Long existingInEdgeCount = existingDocument == null ? null : GetResponseUtil.getFieldValueLong(existingDocument, EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME); if (existingInEdgeCount == null || existingInEdgeCount.intValue() != inEdgeCount) { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME, inEdgeCount); changed = true; } else { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME, existingInEdgeCount.intValue()); } int outEdgeCount = vertex.getEdgeCount(Direction.OUT, authorizations); Long existingOutEdgeCount = existingDocument == null ? null : GetResponseUtil.getFieldValueLong(existingDocument, EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME); if (existingOutEdgeCount == null || existingOutEdgeCount.intValue() != outEdgeCount) { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME, outEdgeCount); changed = true; } else { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME, existingOutEdgeCount.intValue()); } return changed; }
private String[] getParentDocumentFields() { if (this.parentDocumentFields == null) { List<String> fields = new ArrayList<>(); fields.add(ElasticSearchSearchIndexBase.ELEMENT_TYPE_FIELD_NAME); fields.add(VISIBILITY_FIELD_NAME); fields.addAll(getConfig().getScoringStrategy().getFieldNames()); this.parentDocumentFields = fields.toArray(new String[fields.size()]); } return this.parentDocumentFields; }
@Override public boolean addFieldsToVertexDocument(SearchIndex searchIndex, XContentBuilder jsonBuilder, Vertex vertex, GetResponse existingDocument, Authorizations authorizations) throws IOException { if (existingDocument != null && !getConfig().isUpdateEdgeBoost()) { return false; } boolean changed = false; int inEdgeCount = vertex.getEdgeCount(Direction.IN, authorizations); Long existingInEdgeCount = existingDocument == null ? null : GetResponseUtil.getFieldValueLong(existingDocument, EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME); if (existingInEdgeCount == null || existingInEdgeCount.intValue() != inEdgeCount) { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME, inEdgeCount); changed = true; } else { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.IN_EDGE_COUNT_FIELD_NAME, existingInEdgeCount.intValue()); } int outEdgeCount = vertex.getEdgeCount(Direction.OUT, authorizations); Long existingOutEdgeCount = existingDocument == null ? null : GetResponseUtil.getFieldValueLong(existingDocument, EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME); if (existingOutEdgeCount == null || existingOutEdgeCount.intValue() != outEdgeCount) { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME, outEdgeCount); changed = true; } else { jsonBuilder.field(EdgeCountScoringStrategyConfiguration.OUT_EDGE_COUNT_FIELD_NAME, existingOutEdgeCount.intValue()); } return changed; }
@Override public int addElement(ElasticSearchSearchIndexBase searchIndex, Graph graph, BulkRequestWithCount bulkRequestWithCount, IndexInfo indexInfo, Element element, Authorizations authorizations) { int totalCount = 0; if (!getConfig().isUpdateEdgeBoost()) { return totalCount; } if (!(element instanceof Edge)) { return totalCount; } Element vOut = ((Edge) element).getVertex(Direction.OUT, authorizations); if (vOut != null) { searchIndex.addElementToBulkRequest(graph, bulkRequestWithCount.getBulkRequest(), indexInfo, vOut, authorizations); bulkRequestWithCount.incrementCount(); totalCount++; } Element vIn = ((Edge) element).getVertex(Direction.IN, authorizations); if (vIn != null) { searchIndex.addElementToBulkRequest(graph, bulkRequestWithCount.getBulkRequest(), indexInfo, vIn, authorizations); bulkRequestWithCount.incrementCount(); totalCount++; } return totalCount; }
@Override public int addElement(ElasticSearchSearchIndexBase searchIndex, Graph graph, BulkRequestWithCount bulkRequestWithCount, IndexInfo indexInfo, Element element, Authorizations authorizations) { int totalCount = 0; if (!getConfig().isUpdateEdgeBoost()) { return totalCount; } if (!(element instanceof Edge)) { return totalCount; } Element vOut = ((Edge) element).getVertex(Direction.OUT, authorizations); if (vOut != null) { searchIndex.addElementToBulkRequest(graph, bulkRequestWithCount.getBulkRequest(), indexInfo, vOut, authorizations); bulkRequestWithCount.incrementCount(); totalCount++; } Element vIn = ((Edge) element).getVertex(Direction.IN, authorizations); if (vIn != null) { searchIndex.addElementToBulkRequest(graph, bulkRequestWithCount.getBulkRequest(), indexInfo, vIn, authorizations); bulkRequestWithCount.incrementCount(); totalCount++; } return totalCount; }