use of org.elasticsearch.common.lucene.search.MoreLikeThisQuery in project elasticsearch by elastic.
the class MoreLikeThisQueryTests method testSimple.
public void testSimple() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
indexWriter.commit();
Document document = new Document();
document.add(new TextField("_id", "1", Field.Store.YES));
document.add(new TextField("text", "lucene", Field.Store.YES));
indexWriter.addDocument(document);
document = new Document();
document.add(new TextField("_id", "2", Field.Store.YES));
document.add(new TextField("text", "lucene release", Field.Store.YES));
indexWriter.addDocument(document);
IndexReader reader = DirectoryReader.open(indexWriter);
IndexSearcher searcher = new IndexSearcher(reader);
MoreLikeThisQuery mltQuery = new MoreLikeThisQuery("lucene", new String[] { "text" }, Lucene.STANDARD_ANALYZER);
mltQuery.setLikeText("lucene");
mltQuery.setMinTermFrequency(1);
mltQuery.setMinDocFreq(1);
long count = searcher.count(mltQuery);
assertThat(count, equalTo(2L));
reader.close();
indexWriter.close();
}
use of org.elasticsearch.common.lucene.search.MoreLikeThisQuery in project elasticsearch by elastic.
the class MoreLikeThisQueryBuilderTests method testMoreLikeThisBuilder.
public void testMoreLikeThisBuilder() throws Exception {
Query parsedQuery = parseQuery(moreLikeThisQuery(new String[] { "name.first", "name.last" }, new String[] { "something" }, null).minTermFreq(1).maxQueryTerms(12)).toQuery(createShardContext());
assertThat(parsedQuery, instanceOf(MoreLikeThisQuery.class));
MoreLikeThisQuery mltQuery = (MoreLikeThisQuery) parsedQuery;
assertThat(mltQuery.getMoreLikeFields()[0], equalTo("name.first"));
assertThat(mltQuery.getLikeText(), equalTo("something"));
assertThat(mltQuery.getMinTermFrequency(), equalTo(1));
assertThat(mltQuery.getMaxQueryTerms(), equalTo(12));
}
use of org.elasticsearch.common.lucene.search.MoreLikeThisQuery in project elasticsearch by elastic.
the class MoreLikeThisQueryBuilder method doToQuery.
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
Item[] likeItems = new Item[this.likeItems.length];
for (int i = 0; i < likeItems.length; i++) {
likeItems[i] = new Item(this.likeItems[i]);
}
Item[] unlikeItems = new Item[this.unlikeItems.length];
for (int i = 0; i < unlikeItems.length; i++) {
unlikeItems[i] = new Item(this.unlikeItems[i]);
}
MoreLikeThisQuery mltQuery = new MoreLikeThisQuery();
// set similarity
mltQuery.setSimilarity(context.getSearchSimilarity());
// set query parameters
mltQuery.setMaxQueryTerms(maxQueryTerms);
mltQuery.setMinTermFrequency(minTermFreq);
mltQuery.setMinDocFreq(minDocFreq);
mltQuery.setMaxDocFreq(maxDocFreq);
mltQuery.setMinWordLen(minWordLength);
mltQuery.setMaxWordLen(maxWordLength);
mltQuery.setMinimumShouldMatch(minimumShouldMatch);
if (stopWords != null) {
mltQuery.setStopWords(new HashSet<>(Arrays.asList(stopWords)));
}
// sets boost terms
if (boostTerms != 0) {
mltQuery.setBoostTerms(true);
mltQuery.setBoostTermsFactor(boostTerms);
}
// set analyzer
Analyzer analyzerObj = context.getIndexAnalyzers().get(analyzer);
if (analyzerObj == null) {
analyzerObj = context.getMapperService().searchAnalyzer();
}
mltQuery.setAnalyzer(analyzerObj);
// set like text fields
boolean useDefaultField = (fields == null);
List<String> moreLikeFields = new ArrayList<>();
if (useDefaultField) {
moreLikeFields = Collections.singletonList(context.defaultField());
} else {
for (String field : fields) {
MappedFieldType fieldType = context.fieldMapper(field);
if (fieldType != null && SUPPORTED_FIELD_TYPES.contains(fieldType.getClass()) == false) {
if (failOnUnsupportedField) {
throw new IllegalArgumentException("more_like_this only supports text/keyword fields: [" + field + "]");
} else {
// skip
continue;
}
}
moreLikeFields.add(fieldType == null ? field : fieldType.name());
}
}
if (moreLikeFields.isEmpty()) {
return null;
}
mltQuery.setMoreLikeFields(moreLikeFields.toArray(new String[moreLikeFields.size()]));
// handle like texts
if (likeTexts.length > 0) {
mltQuery.setLikeText(likeTexts);
}
if (unlikeTexts.length > 0) {
mltQuery.setUnlikeText(unlikeTexts);
}
// handle items
if (likeItems.length > 0) {
return handleItems(context, mltQuery, likeItems, unlikeItems, include, moreLikeFields, useDefaultField);
} else {
return mltQuery;
}
}
Aggregations