Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public long getNumQueries() {
return results.getNumQueries();
}

private static final class Position {
public static final class Position {
private final long offset;
private final long k;

Expand All @@ -271,7 +271,7 @@ public long getK() {
}
}

private Position getOffsetByIndex(int indexOfTarget) {
public Position getOffsetByIndex(int indexOfTarget) {
List<Long> kList = results.getTopksList();

// if the server didn't return separate topK, use same topK value "0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.vector.request.data.BaseVector;
import io.milvus.v2.service.vector.request.highlighter.Highlighter;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -69,6 +70,9 @@ public class SearchReq {
// Boolean, Long, Double, String, List<Boolean>, List<Long>, List<Double>, List<String>
private Map<String, Object> filterTemplateValues;

// milvus v2.6.9 supports highlighter for search results
private Highlighter highlighter;

private SearchReq(SearchReqBuilder builder) {
this.databaseName = builder.databaseName;
this.collectionName = builder.collectionName;
Expand All @@ -95,6 +99,7 @@ private SearchReq(SearchReqBuilder builder) {
this.functionScore = builder.functionScore;
this.filterTemplateValues = builder.filterTemplateValues;
this.timezone = builder.timezone;
this.highlighter = builder.highlighter;
}

// Getters and Setters
Expand Down Expand Up @@ -294,6 +299,10 @@ public void setFilterTemplateValues(Map<String, Object> filterTemplateValues) {
this.filterTemplateValues = filterTemplateValues;
}

public Highlighter getHighlighter() {
return highlighter;
}

@Override
public String toString() {
return "SearchReq{" +
Expand All @@ -319,6 +328,7 @@ public String toString() {
", groupSize=" + groupSize +
", strictGroupSize=" + strictGroupSize +
", ranker=" + ranker +
", highlighter=" + (highlighter == null ? "null" : (highlighter.highlightType() + ":" + highlighter.getParams())) +
", functionScore=" + functionScore +
// ", filterTemplateValues=" + filterTemplateValues +
'}';
Expand Down Expand Up @@ -354,6 +364,7 @@ public static class SearchReqBuilder {
private CreateCollectionReq.Function ranker;
private FunctionScore functionScore;
private Map<String, Object> filterTemplateValues = new HashMap<>(); // default value
private Highlighter highlighter;

private SearchReqBuilder() {
}
Expand Down Expand Up @@ -487,6 +498,11 @@ public SearchReqBuilder filterTemplateValues(Map<String, Object> filterTemplateV
return this;
}

public SearchReqBuilder highlighter(Highlighter highlighter) {
this.highlighter = highlighter;
return this;
}

public SearchReq build() {
return new SearchReq(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.milvus.v2.service.vector.request.highlighter;

import java.util.Map;

public interface Highlighter {
String highlightType();

Map<String, String> getParams();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.milvus.v2.service.vector.request.highlighter;

import io.milvus.common.utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LexicalHighlighter implements Highlighter {
private final List<HighlightQuery> highlightQueries;
private final Boolean highlightSearchText;
private final List<String> preTags;
private final List<String> postTags;
private final Integer fragmentOffset;
private final Integer fragmentSize;
private final Integer numOfFragments;

public LexicalHighlighter(LexicalHighlighterBuilder builder) {
this.highlightQueries = builder.highlightQueries;
this.highlightSearchText = builder.highlightSearchText;
this.preTags = builder.preTags;
this.postTags = builder.postTags;
this.fragmentOffset = builder.fragmentOffset;
this.fragmentSize = builder.fragmentSize;
this.numOfFragments = builder.numOfFragments;
}

@Override
public String highlightType() {
return "Lexical";
}

@Override
public Map<String, String> getParams() {
Map<String, String> params = new java.util.HashMap<>();
if (this.highlightQueries != null) {
// serialize the list of HighlightQuery to a JSON array string using Gson
params.put("highlight_queries", JsonUtils.toJson(this.highlightQueries));
}
if (this.highlightSearchText != null) {
params.put("highlight_search_text", this.highlightSearchText.toString());
}
if (this.preTags != null) {
params.put("pre_tags", JsonUtils.toJson(this.preTags));
}
if (this.postTags != null) {
params.put("post_tags", JsonUtils.toJson(this.postTags));
}
if (this.fragmentOffset != null) {
params.put("fragment_offset", this.fragmentOffset.toString());
}
if (this.fragmentSize != null) {
params.put("fragment_size", this.fragmentSize.toString());
}
if (this.numOfFragments != null) {
params.put("num_of_fragments", this.numOfFragments.toString());
}
return params;
}

public static class HighlightQuery {
public String type;
public String field;
public String text;

public HighlightQuery(String type, String field, String query) {
this.type = type;
this.field = field;
this.text = query;
}

@Override
public String toString() {
return JsonUtils.toJson(this);
}
}

public static class LexicalHighlighterBuilder {
private List<HighlightQuery> highlightQueries;
private Boolean highlightSearchText;
private List<String> preTags;
private List<String> postTags;
private Integer fragmentOffset;
private Integer fragmentSize;
private Integer numOfFragments;

public LexicalHighlighterBuilder() {
}

public LexicalHighlighterBuilder highlightQueries(List<HighlightQuery> queries) {
this.highlightQueries = queries;
return this;
}

public LexicalHighlighterBuilder addHighlightQuery(HighlightQuery q) {
if (this.highlightQueries == null) this.highlightQueries = new ArrayList<>();
this.highlightQueries.add(q);
return this;
}

public LexicalHighlighterBuilder highlightSearchText(Boolean highlightSearchText) {
this.highlightSearchText = highlightSearchText;
return this;
}

public LexicalHighlighterBuilder preTags(List<String> preTags) {
this.preTags = preTags;
return this;
}

public LexicalHighlighterBuilder addPreTag(String tag) {
if (this.preTags == null) this.preTags = new ArrayList<>();
this.preTags.add(tag);
return this;
}

public LexicalHighlighterBuilder postTags(List<String> postTags) {
this.postTags = postTags;
return this;
}

public LexicalHighlighterBuilder addPostTag(String tag) {
if (this.postTags == null) this.postTags = new ArrayList<>();
this.postTags.add(tag);
return this;
}

public LexicalHighlighterBuilder fragmentOffset(Integer offset) {
this.fragmentOffset = offset;
return this;
}

public LexicalHighlighterBuilder fragmentSize(Integer size) {
this.fragmentSize = size;
return this;
}

public LexicalHighlighterBuilder numOfFragments(Integer num) {
this.numOfFragments = num;
return this;
}

public LexicalHighlighter build() {
return new LexicalHighlighter(this);
}
}

public static LexicalHighlighterBuilder builder() {
return new LexicalHighlighterBuilder();
}
}
Loading
Loading