# MLT using the Java API

**URL:** <https://discuss.elastic.co/t/mlt-using-the-java-api/12242>\
**Category:** Elasticsearch\
**Created:** [June 3, 2013, 8:28pm UTC](https://discuss.elastic.co/t/mlt-using-the-java-api/12242 "2013-06-03T20:28:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [June 3, 2013, 8:28pm UTC](https://discuss.elastic.co/t/mlt-using-the-java-api/12242/1 "2013-06-03T20:28:50Z")

</div>

I'm trying to get the MLT feature working via the Java API, here is my  
request:

SearchResponse searchResponse =  
client.moreLikeThis(moreLikeThisRequest(index).type(type)  
.id(1)  
.fields("text")  
.minTermFreq(1)  
.minDocFreq(1))  
.actionGet();

I've made sure that document 1 and other documents in the index contain the  
word "chicago" in the text field, but it is always returning zero hits.

The 'text' field is indexed and has "term\_vectors:true"

Am I missing something here?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)\
**Post date:** [June 4, 2013, 1:37am UTC](https://discuss.elastic.co/t/mlt-using-the-java-api/12242/2 "2013-06-04T01:37:35Z")

</div>

How many documents are there in the index ?

Try  
.minTermFreq(0)  
.minDocFreq(0))

On Tue, Jun 4, 2013 at 1:58 AM, es newbie [dan.tuffery@gmail.com](mailto:dan.tuffery@gmail.com) wrote:

> I'm trying to get the MLT feature working via the Java API, here is my  
> request:
> 
> SearchResponse searchResponse =  
> client.moreLikeThis(moreLikeThisRequest(index).type(type)  
> .id(1)  
> .fields("text")  
> .minTermFreq(1)  
> .minDocFreq(1))  
> .actionGet();
> 
> I've made sure that document 1 and other documents in the index contain  
> the word "chicago" in the text field, but it is always returning zero hits.
> 
> The 'text' field is indexed and has "term\_vectors:true"
> 
> Am I missing something here?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [June 4, 2013, 8:34am UTC](https://discuss.elastic.co/t/mlt-using-the-java-api/12242/3 "2013-06-04T08:34:15Z")

</div>

Thanks Vineeth.

I got it working in the end. I changed the implementation to use a  
MoreLikeThisQueryBuilder instead.

On Tuesday, June 4, 2013 2:37:35 AM UTC+1, Vineeth Mohan wrote:

> How many documents are there in the index ?
> 
> Try  
> .minTermFreq(0)  
> .minDocFreq(0))
> 
> On Tue, Jun 4, 2013 at 1:58 AM, es newbie \<[dan.t...@gmail.com](mailto:dan.t...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > I'm trying to get the MLT feature working via the Java API, here is my  
> > request:
> > 
> > SearchResponse searchResponse =  
> > client.moreLikeThis(moreLikeThisRequest(index).type(type)  
> > .id(1)  
> > .fields("text")  
> > .minTermFreq(1)  
> > .minDocFreq(1))  
> > .actionGet();
> > 
> > I've made sure that document 1 and other documents in the index contain  
> > the word "chicago" in the text field, but it is always returning zero hits.
> > 
> > The 'text' field is indexed and has "term\_vectors:true"
> > 
> > Am I missing something here?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Chang\_Zhang](https://avatars.discourse-cdn.com/v4/letter/c/4bbf92/32.png) [@Chang\_Zhang](https://discuss.elastic.co/u/Chang_Zhang)\
**Post date:** [June 17, 2013, 3:07pm UTC](https://discuss.elastic.co/t/mlt-using-the-java-api/12242/4 "2013-06-17T15:07:58Z")

</div>

Hello.

I've tried to use MoreLikeThisRequestBuilder with Java API and there was  
always no hit, the code as follows:

```
    MoreLikeThisRequestBuilder mlt = new 

```

MoreLikeThisRequestBuilder(client, "aaa", "bbb", "1");  
mlt.setField("title");  
SearchResponse response =  
client.moreLikeThis(mlt.request()).actionGet();

and I searches your solution, that is using MoreLikeThisQueryBuilder. but  
It doesn't work, either. My codes are:

```
    MoreLikeThisQueryBuilder query = QueryBuilders.moreLikeThisQuery(); 
    query.boost(1.0f).likeText("title").minTermFreq(10);

   SearchResponse searchResponse = 

```

client.prepareSearch("aaa").setTypes("bbb")  
.setQuery(query)  
.execute()  
.actionGet();

is there something wrong with the searchResponse part? could you give me  
some suggestions?  
Thanks in advance!

Am Dienstag, 4. Juni 2013 10:34:15 UTC+2 schrieb es newbie:

> Thanks Vineeth.
> 
> I got it working in the end. I changed the implementation to use a  
> MoreLikeThisQueryBuilder instead.
> 
> On Tuesday, June 4, 2013 2:37:35 AM UTC+1, Vineeth Mohan wrote:
> 
> > How many documents are there in the index ?
> > 
> > Try  
> > .minTermFreq(0)  
> > .minDocFreq(0))
> > 
> > On Tue, Jun 4, 2013 at 1:58 AM, es newbie [dan.t...@gmail.com](mailto:dan.t...@gmail.com) wrote:
> > 
> > > I'm trying to get the MLT feature working via the Java API, here is my  
> > > request:
> > > 
> > > SearchResponse searchResponse =  
> > > client.moreLikeThis(moreLikeThisRequest(index).type(type)  
> > > .id(1)  
> > > .fields("text")  
> > > .minTermFreq(1)  
> > > .minDocFreq(1))  
> > > .actionGet();
> > > 
> > > I've made sure that document 1 and other documents in the index contain  
> > > the word "chicago" in the text field, but it is always returning zero hits.
> > > 
> > > The 'text' field is indexed and has "term\_vectors:true"
> > > 
> > > Am I missing something here?
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dantuff](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@dantuff](https://discuss.elastic.co/u/dantuff)\
**Post date:** [June 17, 2013, 7:35pm UTC](https://discuss.elastic.co/t/mlt-using-the-java-api/12242/5 "2013-06-17T19:35:52Z")

</div>

Hi, your request looks fine.

The value in the 'text' parameter should be the actual text you want to use  
to find similar documents. You have min term frequency set to 10, which  
means there will only be a match if the term "title" appears in a document  
at least 10 times. Also you haven't set the minDocFreq parameters so it  
defaults to 5, which means the term "title" must be present in at least 5  
documents. Are you sure you have documents that have the term "title" in at  
least 10 times, and appears in at least 5 documents? Try decreasing the  
minTermFreq and minDocFreq parameter and experiment with the value in the  
text field.

I have found inquisitor very useful for testing queries:

> **[GitHub - polyfractal/elasticsearch-inquisitor: Site plugin for Elasticsearch...](https://github.com/polyfractal/elasticsearch-inquisitor)**
>
> Site plugin for Elasticsearch to help understand and debug queries. - GitHub - polyfractal/elasticsearch-inquisitor: Site plugin for Elasticsearch to help understand and debug queries.

So in this case  
On Monday, June 17, 2013 4:07:58 PM UTC+1, Chang Zhang wrote:

> Hello.
> 
> I've tried to use MoreLikeThisRequestBuilder with Java API and there was  
> always no hit, the code as follows:
> 
> ```
> MoreLikeThisRequestBuilder mlt = new 
> 
> ```
> 
> MoreLikeThisRequestBuilder(client, "aaa", "bbb", "1");  
> mlt.setField("title");  
> SearchResponse response =  
> client.moreLikeThis(mlt.request()).actionGet();
> 
> and I searches your solution, that is using MoreLikeThisQueryBuilder. but  
> It doesn't work, either. My codes are:
> 
> ```
> MoreLikeThisQueryBuilder query = 
> 
> ```
> 
> QueryBuilders.moreLikeThisQuery();  
> query.boost(1.0f).likeText("title").minTermFreq(10);
> 
> ```
> SearchResponse searchResponse = 
> 
> ```
> 
> client.prepareSearch("aaa").setTypes("bbb")  
> .setQuery(query)  
> .execute()  
> .actionGet();
> 
> is there something wrong with the searchResponse part? could you give me  
> some suggestions?  
> Thanks in advance!
> 
> Am Dienstag, 4. Juni 2013 10:34:15 UTC+2 schrieb es newbie:
> 
> > Thanks Vineeth.
> > 
> > I got it working in the end. I changed the implementation to use a  
> > MoreLikeThisQueryBuilder instead.
> > 
> > On Tuesday, June 4, 2013 2:37:35 AM UTC+1, Vineeth Mohan wrote:
> > 
> > > How many documents are there in the index ?
> > > 
> > > Try  
> > > .minTermFreq(0)  
> > > .minDocFreq(0))
> > > 
> > > On Tue, Jun 4, 2013 at 1:58 AM, es newbie [dan.t...@gmail.com](mailto:dan.t...@gmail.com) wrote:
> > > 
> > > > I'm trying to get the MLT feature working via the Java API, here is my  
> > > > request:
> > > > 
> > > > SearchResponse searchResponse =  
> > > > client.moreLikeThis(moreLikeThisRequest(index).type(type)  
> > > > .id(1)  
> > > > .fields("text")  
> > > > .minTermFreq(1)  
> > > > .minDocFreq(1))  
> > > > .actionGet();
> > > > 
> > > > I've made sure that document 1 and other documents in the index contain  
> > > > the word "chicago" in the text field, but it is always returning zero hits.
> > > > 
> > > > The 'text' field is indexed and has "term\_vectors:true"
> > > > 
> > > > Am I missing something here?
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to the Google  
> > > > Groups "elasticsearch" group.  
> > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > an email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:30am UTC](https://discuss.elastic.co/t/mlt-using-the-java-api/12242/6 "2017-07-06T02:30:45Z")

</div>


