<commit_before>package ru.org.linux.search

import org.springframework.stereotype.Service
import org.springframework.beans.factory.annotation.Autowired
import org.elasticsearch.client.Client
import ru.org.linux.topic.{TopicDao, Topic}
import scala.collection.JavaConversions._
import org.elasticsearch.index.query.QueryBuilders._
import com.typesafe.scalalogging.slf4j.Logging
import org.elasticsearch.index.query.FilterBuilders._

@Service
class MoreLikeThisService @Autowired() (
  client:Client,
  topicDao:TopicDao
) extends Logging {
  // TODO async - return ListenableFuture
  // TODO timeout
  def search(topic:Topic):java.util.List[Topic] = {
    // TODO boost tags
    // see http://stackoverflow.com/questions/15300650/elasticsearch-more-like-this-api-vs-more-like-this-query

    // TODO unescape title
    val titleQuery = moreLikeThisFieldQuery("title")
      .likeText(topic.getTitleUnescaped)
      .minTermFreq(0)
      .minDocFreq(0)

    // TODO tags
    // TODO message body

    // TODO filter out same topic

    val mltQuery = boolQuery()

    mltQuery.should(titleQuery)

    val rootQuery = filteredQuery(mltQuery, termFilter("is_comment", "false"))

    val query = client
      .prepareSearch(SearchQueueListener.MESSAGES_INDEX)
      .setTypes(SearchQueueListener.MESSAGES_TYPE)
      .setQuery(rootQuery)
<commit_msg>не показываем текущий топик в списке похожих<commit_after>package ru.org.linux.search

import org.springframework.stereotype.Service
import org.springframework.beans.factory.annotation.Autowired
import org.elasticsearch.client.Client
import ru.org.linux.topic.{TopicDao, Topic}
import scala.collection.JavaConversions._
import org.elasticsearch.index.query.QueryBuilders._
import com.typesafe.scalalogging.slf4j.Logging
import org.elasticsearch.index.query.FilterBuilders._

@Service
class MoreLikeThisService @Autowired() (
  client:Client,
  topicDao:TopicDao
) extends Logging {
  // TODO async - return ListenableFuture
  // TODO timeout
  // TODO errors
  def search(topic:Topic):java.util.List[Topic] = {
    // TODO boost tags
    // see http://stackoverflow.com/questions/15300650/elasticsearch-more-like-this-api-vs-more-like-this-query

    // TODO unescape title
    val titleQuery = moreLikeThisFieldQuery("title")
      .likeText(topic.getTitleUnescaped)
      .minTermFreq(0)
      .minDocFreq(0)

    // TODO tags
    // TODO message body

    val mltQuery = boolQuery()

    mltQuery.should(titleQuery)

    val rootFilter = boolFilter()
    rootFilter.must(termFilter("is_comment", "false"))
    rootFilter.mustNot(idsFilter(SearchQueueListener.MESSAGES_TYPE).addIds(topic.getId.toString))

    val rootQuery = filteredQuery(mltQuery, rootFilter)

    val query = client
      .prepareSearch(SearchQueueListener.MESSAGES_INDEX)
      .setTypes(SearchQueueListener.MESSAGES_TYPE)
      .setQuery(rootQuery)

{'fraction_non_alphanumeric': 0.10216178521617852, 'fraction_numerical': 0.007670850767085077, 'mean_word_length': 4.068904593639576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6077716', 'n_tokens_mistral': 896, 'n_tokens_neox': 887, 'n_words': 156}