use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.
the class MoreLikeThisIT method testMoreLikeThisMalformedArtificialDocs.
public void testMoreLikeThisMalformedArtificialDocs() throws Exception {
logger.info("Creating the index ...");
assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text,analyzer=whitespace", "date", "type=date"));
ensureGreen("test");
logger.info("Creating an index with a single document ...");
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("text", "Hello World!").field("date", "2009-01-01").endObject()));
logger.info("Checking with a malformed field value ...");
XContentBuilder malformedFieldDoc = jsonBuilder().startObject().field("text", "Hello World!").field("date", "this is not a date!").endObject();
MoreLikeThisQueryBuilder mltQuery = moreLikeThisQuery(new Item[] { new Item("test", "type1", malformedFieldDoc) }).minTermFreq(0).minDocFreq(0).minimumShouldMatch("0%");
SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
assertSearchResponse(response);
assertHitCount(response, 0);
logger.info("Checking with an empty document ...");
XContentBuilder emptyDoc = jsonBuilder().startObject().endObject();
mltQuery = moreLikeThisQuery(null, new Item[] { new Item("test", "type1", emptyDoc) }).minTermFreq(0).minDocFreq(0).minimumShouldMatch("0%");
response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
assertSearchResponse(response);
assertHitCount(response, 0);
logger.info("Checking the document matches otherwise ...");
XContentBuilder normalDoc = jsonBuilder().startObject().field("text", "Hello World!").field("date", // should be properly parsed but ignored ...
"1000-01-01").endObject();
mltQuery = moreLikeThisQuery(null, new Item[] { new Item("test", "type1", normalDoc) }).minTermFreq(0).minDocFreq(0).minimumShouldMatch(// strict all terms must match but date is ignored
"100%");
response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
assertSearchResponse(response);
assertHitCount(response, 1);
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.
the class MoreLikeThisIT method testSimpleMoreLikeOnLongField.
public void testSimpleMoreLikeOnLongField() throws Exception {
logger.info("Creating index test");
assertAcked(prepareCreate("test").addMapping("type1", "some_long", "type=long"));
logger.info("Running Cluster Health");
assertThat(ensureGreen(), equalTo(ClusterHealthStatus.GREEN));
logger.info("Indexing...");
client().index(indexRequest("test").type("type1").id("1").source(jsonBuilder().startObject().field("some_long", 1367484649580L).endObject())).actionGet();
client().index(indexRequest("test").type("type2").id("2").source(jsonBuilder().startObject().field("some_long", 0).endObject())).actionGet();
client().index(indexRequest("test").type("type1").id("3").source(jsonBuilder().startObject().field("some_long", -666).endObject())).actionGet();
client().admin().indices().refresh(refreshRequest()).actionGet();
logger.info("Running moreLikeThis");
SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("test", "type1", "1") }).minTermFreq(1).minDocFreq(1)).get();
assertHitCount(response, 0L);
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.
the class MoreLikeThisIT method testMoreLikeThisMultiValueFields.
public void testMoreLikeThisMultiValueFields() throws Exception {
logger.info("Creating the index ...");
assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text,analyzer=keyword").setSettings(SETTING_NUMBER_OF_SHARDS, 1));
ensureGreen();
logger.info("Indexing ...");
String[] values = { "aaaa", "bbbb", "cccc", "dddd", "eeee", "ffff", "gggg", "hhhh", "iiii", "jjjj" };
List<IndexRequestBuilder> builders = new ArrayList<>(values.length + 1);
// index one document with all the values
builders.add(client().prepareIndex("test", "type1", "0").setSource("text", values));
// index each document with only one of the values
for (int i = 0; i < values.length; i++) {
builders.add(client().prepareIndex("test", "type1", String.valueOf(i + 1)).setSource("text", values[i]));
}
indexRandom(true, builders);
int maxIters = randomIntBetween(10, 20);
for (int i = 0; i < maxIters; i++) {
int max_query_terms = randomIntBetween(1, values.length);
logger.info("Running More Like This with max_query_terms = {}", max_query_terms);
MoreLikeThisQueryBuilder mltQuery = moreLikeThisQuery(new String[] { "text" }, null, new Item[] { new Item(null, null, "0") }).minTermFreq(1).minDocFreq(1).maxQueryTerms(max_query_terms).minimumShouldMatch("0%");
SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).execute().actionGet();
assertSearchResponse(response);
assertHitCount(response, max_query_terms);
}
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.
the class MoreLikeThisIT method testMinimumShouldMatch.
public void testMinimumShouldMatch() throws ExecutionException, InterruptedException {
logger.info("Creating the index ...");
assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text,analyzer=whitespace").setSettings(SETTING_NUMBER_OF_SHARDS, 1));
ensureGreen();
logger.info("Indexing with each doc having one less term ...");
List<IndexRequestBuilder> builders = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String text = "";
for (int j = 1; j <= 10 - i; j++) {
text += j + " ";
}
builders.add(client().prepareIndex("test", "type1", i + "").setSource("text", text));
}
indexRandom(true, builders);
logger.info("Testing each minimum_should_match from 0% - 100% with 10% increment ...");
for (int i = 0; i <= 10; i++) {
String minimumShouldMatch = (10 * i) + "%";
MoreLikeThisQueryBuilder mltQuery = moreLikeThisQuery(new String[] { "text" }, new String[] { "1 2 3 4 5 6 7 8 9 10" }, null).minTermFreq(1).minDocFreq(1).minimumShouldMatch(minimumShouldMatch);
logger.info("Testing with minimum_should_match = {}", minimumShouldMatch);
SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
assertSearchResponse(response);
if (minimumShouldMatch.equals("0%")) {
assertHitCount(response, 10);
} else {
assertHitCount(response, 11 - i);
}
}
}
use of org.elasticsearch.index.query.MoreLikeThisQueryBuilder in project elasticsearch by elastic.
the class MoreLikeThisIT method testMoreLikeThisIssueRoutingNotSerialized.
// Issue #3039
public void testMoreLikeThisIssueRoutingNotSerialized() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("bar").startObject("properties").endObject().endObject().endObject().string();
assertAcked(prepareCreate("foo", 2, Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 2).put(SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("bar", mapping, XContentType.JSON));
ensureGreen();
client().prepareIndex("foo", "bar", "1").setSource(jsonBuilder().startObject().startObject("foo").field("bar", "boz").endObject().endObject()).setRouting("4000").execute().actionGet();
client().admin().indices().prepareRefresh("foo").execute().actionGet();
SearchResponse response = client().prepareSearch().setQuery(new MoreLikeThisQueryBuilder(null, new Item[] { new Item("foo", "bar", "1").routing("4000") })).get();
assertNoFailures(response);
assertThat(response, notNullValue());
}
Aggregations