Search in sources :

Example 1 with MoreLikeThisQuery

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) MoreLikeThisQuery(org.elasticsearch.common.lucene.search.MoreLikeThisQuery) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) RAMDirectory(org.apache.lucene.store.RAMDirectory) RAMDirectory(org.apache.lucene.store.RAMDirectory) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 2 with MoreLikeThisQuery

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));
}
Also used : Query(org.apache.lucene.search.Query) QueryBuilders.moreLikeThisQuery(org.elasticsearch.index.query.QueryBuilders.moreLikeThisQuery) MoreLikeThisQuery(org.elasticsearch.common.lucene.search.MoreLikeThisQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MoreLikeThisQuery(org.elasticsearch.common.lucene.search.MoreLikeThisQuery)

Example 3 with MoreLikeThisQuery

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;
    }
}
Also used : MoreLikeThisQuery(org.elasticsearch.common.lucene.search.MoreLikeThisQuery) ArrayList(java.util.ArrayList) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Analyzer(org.apache.lucene.analysis.Analyzer)

Aggregations

MoreLikeThisQuery (org.elasticsearch.common.lucene.search.MoreLikeThisQuery)3 ArrayList (java.util.ArrayList)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 Document (org.apache.lucene.document.Document)1 TextField (org.apache.lucene.document.TextField)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 Directory (org.apache.lucene.store.Directory)1 RAMDirectory (org.apache.lucene.store.RAMDirectory)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1 QueryBuilders.moreLikeThisQuery (org.elasticsearch.index.query.QueryBuilders.moreLikeThisQuery)1