本文整理汇总了Java中org.elasticsearch.search.rescore.QueryRescoreMode类的典型用法代码示例。如果您正苦于以下问题:Java QueryRescoreMode类的具体用法?Java QueryRescoreMode怎么用?Java QueryRescoreMode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
QueryRescoreMode类属于org.elasticsearch.search.rescore包,在下文中一共展示了QueryRescoreMode类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import org.elasticsearch.search.rescore.QueryRescoreMode; //导入依赖的package包/类
@Test
public void test() throws Exception {
SearchResponse searchResponse = client.prepareSearch()
.setIndices("test")
.setTypes("test")
.setQuery(QueryBuilders.matchQuery("name","天津公安"))
.addRescorer(new QueryRescorerBuilder(QueryBuilders.matchPhraseQuery("name","天津公安")))
.addRescorer(new QueryRescorerBuilder(
QueryBuilders.functionScoreQuery(
ScoreFunctionBuilders.scriptFunction("doc['time'].value / 10000")
)
).windowSize(100).setScoreMode(QueryRescoreMode.Multiply))
.setFrom(0)
.setSize(100)
.execute()
.actionGet();
}
示例2: testMultipleRescores
import org.elasticsearch.search.rescore.QueryRescoreMode; //导入依赖的package包/类
public void testMultipleRescores() throws Exception {
int numDocs = indexRandomNumbers("keyword", 1, true);
QueryRescorerBuilder eightIsGreat = RescoreBuilder
.queryRescorer(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", English.intToEnglish(8)),
ScoreFunctionBuilders.weightFactorFunction(1000.0f)).boostMode(CombineFunction.REPLACE))
.setScoreMode(QueryRescoreMode.Total);
QueryRescorerBuilder sevenIsBetter = RescoreBuilder
.queryRescorer(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("field1", English.intToEnglish(7)),
ScoreFunctionBuilders.weightFactorFunction(10000.0f)).boostMode(CombineFunction.REPLACE))
.setScoreMode(QueryRescoreMode.Total);
// First set the rescore window large enough that both rescores take effect
SearchRequestBuilder request = client().prepareSearch();
request.addRescorer(eightIsGreat, numDocs).addRescorer(sevenIsBetter, numDocs);
SearchResponse response = request.get();
assertFirstHit(response, hasId("7"));
assertSecondHit(response, hasId("8"));
// Now squash the second rescore window so it never gets to see a seven
response = request.setSize(1).clearRescorers().addRescorer(eightIsGreat, numDocs).addRescorer(sevenIsBetter, 1).get();
assertFirstHit(response, hasId("8"));
// We have no idea what the second hit will be because we didn't get a chance to look for seven
// Now use one rescore to drag the number we're looking for into the window of another
QueryRescorerBuilder ninetyIsGood = RescoreBuilder.queryRescorer(QueryBuilders
.functionScoreQuery(QueryBuilders.queryStringQuery("*ninety*"), ScoreFunctionBuilders.weightFactorFunction(1000.0f))
.boostMode(CombineFunction.REPLACE)).setScoreMode(QueryRescoreMode.Total);
QueryRescorerBuilder oneToo = RescoreBuilder.queryRescorer(QueryBuilders
.functionScoreQuery(QueryBuilders.queryStringQuery("*one*"), ScoreFunctionBuilders.weightFactorFunction(1000.0f))
.boostMode(CombineFunction.REPLACE)).setScoreMode(QueryRescoreMode.Total);
request.clearRescorers().addRescorer(ninetyIsGood, numDocs).addRescorer(oneToo, 10);
response = request.setSize(2).get();
assertThat(response.getHits().getMaxScore(), equalTo(response.getHits().getHits()[0].getScore()));
assertFirstHit(response, hasId("91"));
assertFirstHit(response, hasScore(2001.0f));
assertSecondHit(response, hasScore(1001.0f)); // Not sure which one it is but it is ninety something
}
示例3: testScoring
import org.elasticsearch.search.rescore.QueryRescoreMode; //导入依赖的package包/类
public void testScoring() throws Exception {
int numDocs = indexRandomNumbers("keyword");
String[] scoreModes = new String[]{ "max", "min", "avg", "total", "multiply", "" };
float primaryWeight = 1.1f;
float secondaryWeight = 1.6f;
for (String scoreMode : scoreModes) {
for (int i = 0; i < numDocs - 4; i++) {
String[] intToEnglish = new String[] { English.intToEnglish(i), English.intToEnglish(i + 1), English.intToEnglish(i + 2),
English.intToEnglish(i + 3) };
QueryBuilder query = boolQuery().disableCoord(true)
.should(functionScoreQuery(termQuery("field1", intToEnglish[0]), weightFactorFunction(2.0f)).boostMode(REPLACE))
.should(functionScoreQuery(termQuery("field1", intToEnglish[1]), weightFactorFunction(3.0f)).boostMode(REPLACE))
.should(functionScoreQuery(termQuery("field1", intToEnglish[2]), weightFactorFunction(5.0f)).boostMode(REPLACE))
.should(functionScoreQuery(termQuery("field1", intToEnglish[3]), weightFactorFunction(0.2f)).boostMode(REPLACE));
QueryRescorerBuilder rescoreQuery = queryRescorer(boolQuery().disableCoord(true)
.should(functionScoreQuery(termQuery("field1", intToEnglish[0]), weightFactorFunction(5.0f)).boostMode(REPLACE))
.should(functionScoreQuery(termQuery("field1", intToEnglish[1]), weightFactorFunction(7.0f)).boostMode(REPLACE))
.should(functionScoreQuery(termQuery("field1", intToEnglish[3]), weightFactorFunction(0.0f)).boostMode(REPLACE)));
rescoreQuery.setQueryWeight(primaryWeight).setRescoreQueryWeight(secondaryWeight);
if (!"".equals(scoreMode)) {
rescoreQuery.setScoreMode(QueryRescoreMode.fromString(scoreMode));
}
SearchResponse rescored = client().prepareSearch()
.setPreference("test") // ensure we hit the same shards for tie-breaking
.setFrom(0).setSize(10).setQuery(query).setRescorer(rescoreQuery, 50).get();
assertHitCount(rescored, 4);
assertThat(rescored.getHits().getMaxScore(), equalTo(rescored.getHits().getHits()[0].getScore()));
if ("total".equals(scoreMode) || "".equals(scoreMode)) {
assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
assertSecondHit(rescored, hasId(String.valueOf(i)));
assertThirdHit(rescored, hasId(String.valueOf(i + 2)));
assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(3.0f * primaryWeight + 7.0f * secondaryWeight));
assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(2.0f * primaryWeight + 5.0f * secondaryWeight));
assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(5.0f * primaryWeight));
assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.2f * primaryWeight + 0.0f * secondaryWeight));
} else if ("max".equals(scoreMode)) {
assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
assertSecondHit(rescored, hasId(String.valueOf(i)));
assertThirdHit(rescored, hasId(String.valueOf(i + 2)));
assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(7.0f * secondaryWeight));
assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(5.0f * secondaryWeight));
assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(5.0f * primaryWeight));
assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.2f * primaryWeight));
} else if ("min".equals(scoreMode)) {
assertFirstHit(rescored, hasId(String.valueOf(i + 2)));
assertSecondHit(rescored, hasId(String.valueOf(i + 1)));
assertThirdHit(rescored, hasId(String.valueOf(i)));
assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(5.0f * primaryWeight));
assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(3.0f * primaryWeight));
assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(2.0f * primaryWeight));
assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.0f * secondaryWeight));
} else if ("avg".equals(scoreMode)) {
assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
assertSecondHit(rescored, hasId(String.valueOf(i + 2)));
assertThirdHit(rescored, hasId(String.valueOf(i)));
assertThat(rescored.getHits().getHits()[0].getScore(), equalTo((3.0f * primaryWeight + 7.0f * secondaryWeight) / 2.0f));
assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(5.0f * primaryWeight));
assertThat(rescored.getHits().getHits()[2].getScore(), equalTo((2.0f * primaryWeight + 5.0f * secondaryWeight) / 2.0f));
assertThat(rescored.getHits().getHits()[3].getScore(), equalTo((0.2f * primaryWeight) / 2.0f));
} else if ("multiply".equals(scoreMode)) {
assertFirstHit(rescored, hasId(String.valueOf(i + 1)));
assertSecondHit(rescored, hasId(String.valueOf(i)));
assertThirdHit(rescored, hasId(String.valueOf(i + 2)));
assertThat(rescored.getHits().getHits()[0].getScore(), equalTo(3.0f * primaryWeight * 7.0f * secondaryWeight));
assertThat(rescored.getHits().getHits()[1].getScore(), equalTo(2.0f * primaryWeight * 5.0f * secondaryWeight));
assertThat(rescored.getHits().getHits()[2].getScore(), equalTo(5.0f * primaryWeight));
assertThat(rescored.getHits().getHits()[3].getScore(), equalTo(0.2f * primaryWeight * 0.0f * secondaryWeight));
}
}
}
}
示例4: testFullUsecase
import org.elasticsearch.search.rescore.QueryRescoreMode; //导入依赖的package包/类
public void testFullUsecase() throws Exception {
addElement(new StoredFeature("feature1", Collections.singletonList("query"), "mustache",
QueryBuilders.matchQuery("field1", "{{query}}").toString()));
addElement(new StoredFeature("feature2", Collections.singletonList("query"), "mustache",
QueryBuilders.matchQuery("field2", "{{query}}").toString()));
addElement(new StoredFeature("feature3", Collections.singletonList("query"), "derived_expression",
"(feature1 - feature2) > 0 ? 1 : -1"));
AddFeaturesToSetRequestBuilder builder = AddFeaturesToSetAction.INSTANCE.newRequestBuilder(client());
builder.request().setFeatureSet("my_set");
builder.request().setFeatureNameQuery("feature1");
builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
builder.execute().get();
builder.request().setFeatureNameQuery("feature2");
builder.execute().get();
builder.request().setFeatureNameQuery("feature3");
long version = builder.get().getResponse().getVersion();
CreateModelFromSetRequestBuilder createModelFromSetRequestBuilder = CreateModelFromSetAction.INSTANCE.newRequestBuilder(client());
createModelFromSetRequestBuilder.withVersion(IndexFeatureStore.DEFAULT_STORE, "my_set", version,
"my_model", new StoredLtrModel.LtrModelDefinition("model/linear", SIMPLE_MODEL, true));
createModelFromSetRequestBuilder.get();
buildIndex();
Map<String, Object> params = new HashMap<>();
boolean negativeScore = random().nextBoolean();
params.put("query", negativeScore ? "bonjour" : "hello");
SearchRequestBuilder sb = client().prepareSearch("test_index")
.setQuery(QueryBuilders.matchQuery("field1", "world"))
.setRescorer(RescoreBuilder
.queryRescorer(new WrapperQueryBuilder(new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
.modelName("my_model").params(params).toString()))
.setScoreMode(QueryRescoreMode.Total)
.setQueryWeight(0)
.setRescoreQueryWeight(1));
SearchResponse sr = sb.get();
assertEquals(1, sr.getHits().getTotalHits());
if(negativeScore) {
assertThat(sr.getHits().getAt(0).getScore(), Matchers.lessThan(-10.0f));
} else {
assertThat(sr.getHits().getAt(0).getScore(), Matchers.greaterThan(10.0f));
}
StoredLtrModel model = getElement(StoredLtrModel.class, StoredLtrModel.TYPE, "my_model");
CachesStatsNodesResponse stats = CachesStatsAction.INSTANCE.newRequestBuilder(client()).execute().get();
assertEquals(1, stats.getAll().getTotal().getCount());
assertEquals(model.compile(parserFactory()).ramBytesUsed(), stats.getAll().getTotal().getRam());
assertEquals(1, stats.getAll().getModels().getCount());
assertEquals(model.compile(parserFactory()).ramBytesUsed(), stats.getAll().getModels().getRam());
assertEquals(0, stats.getAll().getFeatures().getCount());
assertEquals(0, stats.getAll().getFeatures().getRam());
assertEquals(0, stats.getAll().getFeaturesets().getCount());
assertEquals(0, stats.getAll().getFeaturesets().getRam());
ClearCachesAction.RequestBuilder clearCache = ClearCachesAction.INSTANCE.newRequestBuilder(client());
clearCache.request().clearModel(IndexFeatureStore.DEFAULT_STORE, "my_model");
clearCache.get();
stats = CachesStatsAction.INSTANCE.newRequestBuilder(client()).execute().get();
assertEquals(0, stats.getAll().getTotal().getCount());
assertEquals(0, stats.getAll().getTotal().getRam());
}