<commit_before>from flask import request
from flask import json
from app import app
from elasticsearch import Elasticsearch
import os

host = 'http://{}:{}@ip-10-0-0-10:9200/'.format(os.environ['ELASTIC_USER'], os.environ['ELASTIC_PASS'])
es = Elasticsearch([host], timeout=90)

@app.route('/topics', methods=['POST'])
def get_recommendations():
    data = request.get_json()
    topics = data['topics']
    simple = data.get('simple', True)
    if simple:
        return json.dumps(query_simple(topics))
    else:
        return json.dumps(query_custom(topics))

def query_custom(topics):
    should_clause = [
        {'constant_score': {
            'filter' : {
                'match': { 'topics' :  topic} }}}
        for topic in topics
    ]
    query = {
        'query': {
            'function_score': {
                'query': {
                    'bool': {'should': should_clause }
                },
                'script_score' : {
                    'script' : {
                        'inline': '- Math.abs(_score/doc[\'num_topics\'].value - 0.75)'
                    }<commit_msg>add random document endpoint<commit_after>from flask import request
from flask import json
from app import app
from elasticsearch import Elasticsearch
import os
from random import randint

host = 'http://{}:{}@ip-10-0-0-10:9200/'.format(os.environ['ELASTIC_USER'], os.environ['ELASTIC_PASS'])
es = Elasticsearch([host], timeout=90)

@app.route('/topics', methods=['POST'])
def get_recommendations():
    data = request.get_json()
    topics = data['topics']
    simple = data.get('simple', True)
    if simple:
        return json.dumps(query_simple(topics))
    else:
        return json.dumps(query_custom(topics))

@app.route('/random')
def get_random():
    seed = randint(-10**6, 10**6)
    query = {
       "size": 1,
       "query": {
          "function_score": {
             "functions": [
                {
                   "random_score": {
                      "seed": seed
                   }
                }
             ]
          }
       }
    }
    return json.dumps(_exec_query(query))


def query_custom(topics):
    should_clause = [
        {'constant_score': {
            'filter' : {
                'match': { 'topics' :  topic} }}}
        for topic in topics
    ]
    query = {
        'query': {
            'function_score': {
                'query': {
                    'bool': {'should': should_clause }
                },
                'script_score' : {
                    'script' : {
                        'inline': '- Math.abs(_score/doc[\'num_topics\'].value - 0.75)'
                    }
{'fraction_non_alphanumeric': 0.144960362400906, 'fraction_numerical': 0.013967534918837296, 'mean_word_length': 1.7894736842105263, 'pattern_counts': {'":': 6, '<': 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': '4070328', 'n_tokens_mistral': 825, 'n_tokens_neox': 787, 'n_words': 156}