def test_query_dsl_with_without_valid_query_field():
    query = {
        "query": {
            "bool": {
                "must": [{
                    "match": {
                        "title": "Search"
                    }
                }, {
                    "match": {
                        "content": "Elasticsearch"
                    }
                }],
                "filter": [{
                    "term": {
                        "status": "published"
                    }
                }, {
                    "range": {
                        "publish_date": {
                            "gte": "2015-01-01"
                        }
                    }
                }]
            }
        }
    }
    with pytest.raises(Exception):
        Question.from_elastic_query_dsl(query)
def test_query_dsl_with_without_multiple_query_field():
    query = {
        "query": {
            "bool": {
                "should": [{
                    "match": {
                        "name.first": {
                            "query": "shay",
                            "_name": "first"
                        }
                    }
                }, {
                    "match": {
                        "name.last": {
                            "query": "banon",
                            "_name": "last"
                        }
                    }
                }],
                "filter": {
                    "terms": {
                        "name.last": ["banon", "kimchy"],
                        "_name": "test"
                    }
                }
            }
        }
    }
    with pytest.raises(Exception):
        Question.from_elastic_query_dsl(query)
Esempio n. 3
0
def faq_qa(model_id: int, request: Question):
    finder = FINDERS.get(model_id, None)
    if not finder:
        raise HTTPException(
            status_code=404,
            detail=
            f"Couldn't get Finder with ID {model_id}. Available IDs: {list(FINDERS.keys())}"
        )

    results = []
    for question in request.questions:
        if request.filters:
            # put filter values into a list and remove filters with null value
            filters = {
                key: [value]
                for key, value in request.filters.items() if value is not None
            }
            logger.info(f" [{datetime.now()}] Request: {request}")
        else:
            filters = {}

        result = finder.get_answers_via_similar_questions(
            question=question,
            top_k_retriever=request.top_k_retriever,
            filters=filters,
        )
        results.append(result)

    elasticapm.set_custom_context({"results": results})
    logger.info(json.dumps({"request": request.dict(), "results": results}))

    return {"results": results}
def test_query_dsl_with_filter():
    query = {
        "query": {
            "bool": {
                "should": [{
                    "match": {
                        "name.first": {
                            "query": "shay",
                            "_name": "first"
                        }
                    }
                }],
                "filter": {
                    "terms": {
                        "name.last": ["banon", "kimchy"],
                        "_name": "test"
                    }
                }
            }
        }
    }
    question = Question.from_elastic_query_dsl(query)
    assert 1 == len(question.questions)
    assert question.questions.__contains__("shay")
    assert len(question.filters) == 1
    assert question.filters["_name"] == "test"
Esempio n. 5
0
def query(model_id: int, query_request: Dict[str, Any], top_k_reader: int = DEFAULT_TOP_K_READER):
    with doc_qa_limiter.run():
        start_time = time.time()
        finder = FINDERS.get(model_id, None)
        if not finder:
            raise HTTPException(
                status_code=404, detail=f"Could not get Finder with ID {model_id}. Available IDs: {list(FINDERS.keys())}"
            )

        question_request = Question.from_elastic_query_dsl(query_request, top_k_reader)

        answers = search_documents(finder, question_request, start_time)
        response: Dict[str, Any] = {}
        if answers and len(answers) > 0:
            response = AnswersToIndividualQuestion.to_elastic_response_dsl(dict(answers[0]))

        return response
def test_query_dsl_with_complex_query():
    query = {
        "size": 17,
        "query": {
            "bool": {
                "should": [{
                    "multi_match": {
                        "query": "I am test1",
                        "type": "most_fields",
                        "fields": ["text", "title"]
                    }
                }],
                "filter": [{
                    "terms": {
                        "year": "2020"
                    }
                }, {
                    "terms": {
                        "quarter": "1"
                    }
                }, {
                    "range": {
                        "date": {
                            "gte": "12-12-12"
                        }
                    }
                }]
            }
        }
    }
    top_k_reader = 7
    question = Question.from_elastic_query_dsl(query, top_k_reader)
    assert 1 == len(question.questions)
    assert question.questions.__contains__("I am test1")
    assert 2 == len(question.filters)
    assert question.filters["year"] == "2020"
    assert question.filters["quarter"] == "1"
    assert 17 == question.top_k_retriever
    assert 7 == question.top_k_reader
def test_query_dsl_with_single_query():
    query = {"query": {"match": {"message": {"query": "this is a test"}}}}
    question = Question.from_elastic_query_dsl(query)
    assert 1 == len(question.questions)
    assert question.questions.__contains__("this is a test")
    assert question.filters is None