You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

The Query Stage is a crucial component in SearchAPI that enables you to perform powerful search queries on an index or alias. It provides a wide range of options and parameters to customize and fine-tune your search.

With the Query Stage, you can specify the fields to be used for matching terms, phrases, spans, date ranges, and ranges. This allows you to define precisely how your search results are retrieved and filtered based on your specific requirements.

One of the key features of the Query Stage is the ability to leverage PyQPL (Query Parser Language) to create complex and advanced queries. PyQPL provides a flexible syntax that allows you to construct intricate search conditions, combine multiple search criteria, and define custom operators for precise control over your search results.


The Query Stage also supports various additional functionalities to enhance your search experience. These include pagination, sorting, highlighting (to emphasize search terms in the results), filters (to narrow down search results based on specific criteria), aggregations (to obtain statistical insights from search results), and the ability to fetch specific fields to optimize performance and reduce data transfer.

Properties

PropertyDescriptionDefaultTypeRequiredQPL Config?
enableEnable stage for executiontruebooleanNoNo
nameName for this specific stage"vector"stringNoNo
save_to_intermediateIf true, the result of the stage will be stored in the intermediate instead of the final sectionfalsebooleanNoNo
expand_resultIndicates if the result of this stage should be expanded into the final data dictionary instead of being appended as usualfalsebooleanNoNo
halt_on_exceptionIndicates if the pipeline should be interrupted in case of an exceptiontruebooleanNoNo
fieldsFields to be used for matching terms, phrases, spans, etc.
array, object, or stringYesYes
model

EnumYesNo
vector_field_name

stringYesNo


Query Stage Intermediate Parameters

The Query Stage offers a range of parameters that can be passed via the intermediate input to customize your search request or modify the configuration of the current stage. These parameters provide flexibility and control over the search process.


ParameterDescription
qA string query for performing a search. Can be transformed into engine-specific queries using PyQPL (Query Parser Language).
queryEngine-specific queries for the search.
knnEngine-specific queries specifically for k-nearest neighbor (KNN) searches.
sizeNumber of results to return from the search request. Overrides the size specified in the configuration.
from/startIndicates the starting point for retrieving search results. Can be used interchangeably with the page parameter.
pageIt can be an alternative to from/start. It calculates the start based on the size parameter
fetch_fieldsList of fields to fetch for each search result. Overrides the fields specified in the configuration.
exclude_fieldsList of fields to exclude from the search results. Overrides the fields specified in the configuration.
scrollScroll ID used to retrieve large numbers of results from a single search request, similar to a cursor in a traditional database.
operatorThe default operator for query string queries: AND or OR. Overrides the default operator specified in the configuration.

Remember that the intermediate can be fill with either other stages or the original request body that trigger the pipeline, making this essentially REST API parameters

Additional Classes for Configuration

QueryStageUIConfig 

The QueryStageUIConfig is a configuration object that provides UI-specific settings for the QueryStage.

QueryStageUIConfig Properties

PropertyDescriptionDefaultType
page_sizeConfiguration for page size settings in the UIVectorNo
sortConfiguration for sort settings in the UIVectorNo

PageSizeConfig Properties 

PropertyDescriptionDefaultType
defaultDefault page size value25integer
optionsAvailable page size options[25, 50, 100]array of integers

SortConfig Properties 

PropertyDescriptionTypeRequired
defaultDefault sort entrySortEntryYes
optionsAvailable sort optionsarray of VectorYes

SortEntry Properties 

PropertyDescriptionTypeRequired
fieldName of the field to be used for sortingstringYes
display_nameDisplay name for this sort entry (only applicable for user interface)stringNo
orderSort order to be usedVector or objectYes

SortOrder Enum 

Enum ValueDescription
"asc"Ascending sort order
"desc"Descending sort order




Example Configuration

synonyms = {
            'cancer': ['cancer', 'malignancy', '363346000', 'cancers', 'malignancies', '"malignant growth"',
                       '"malignant neoplasm"', '"malignant neoplasms"', '"malignant neoplastic disease"',
                       '"malignant tumor"', '"malignant tumors"', '"neoplasm malignant"', '"neoplasm/cancer"',
                       '"tumor, malignant"'],
            'headache': ['headache', '25064002', 'cephalalgia', 'cephalgia', 'cephalgias', '"cranial pain"',
                         '"have headaches"', '"head ache"', '"head pain"', '"head pain cephalgia"', '"head pains"',
                         'headaches', '"mild global headache"', '"mild headache"', '"pain head"', '"pain in head"',
                         '"pain, head"']
        }

_query_stage = QueryStage(
    engine_name=DEFAULT_ENGINE_NAME,
    enable=True,
    qpl_enable=True,
    name=SEARCH_STAGE_NAME,
    index='movies',
    synonyms_call= lambda x: self.synonyms.get(x, None), # Simple synonym implementation
    wildcard=False,
    fields=['title', 'overview', 'url', 'status', 'metadata', 'metadata.production_companies',
            'metadata.cast',
            'metadata.directors'],
    range_fields=['metadata.budget'],
    date_fields=['release_date'],
    fetch_fields=['title', 'adult', 'overview', 'url', 'release_date', 'status', 'video', 'metadata',
                  'metadata.production_companies', 'metadata.cast', 'metadata.directors'],
    exclude_fields=None,
    implicit_operator='or',
    page_size_default=25,
    sort=SortEntry(
        field='_score',
        order=SortOrder.DESC
    ),
    aggregations=_aggregation_stage.name,
    aggregations_filters=_aggregation_stage.filters_name,
    highlight=_highlight_stage.name,
    filters=_filter_stage.name,
    ui_only=QueryStageUIConfig(
        sort=SortConfig(
            default=SortEntry(
                field='_score',
                display_name='Score',
                order=SortOrder.DESC
            ),
            options=[
                SortEntry(
                    field='_score',
                    display_name='Score',
                    order=SortOrder.DESC
                ),
                SortEntry(
                    field='release_date',
                    display_name='Release Date',
                    order=SortOrder.ASC
                ),
                SortEntry(
                    field='metadata.budget',
                    display_name='Budget',
                    order=SortOrder.DESC
                )
            ]
        ),
        page_size=PageSizeConfig(
            default=25,
            options=[25, 50, 100]
        )
    )
)
  • No labels