ELSER hybrid search. Error: '[sub_searches] query malformed, no start_object after query name')

I am trying a develop a hybrid search query by combing ELSER Semantic and Full Text searches using Python client. Below is my sample code. I am getting a error

'[sub_searches] query malformed, no start_object after query name')

Looks i am missing some think in Syntax. Any help is welcome.

query_text ="CMOS"
index_Name_emb="sales_test"
emdeddingmodel=".elser_model_2"

resp = client.search(index=index_Name_emb,
                query={
                        "sub_searches":[
                        {
                            "query":{
                            "multi_match" : {
                            "query":query_text,
                            "fields": ["content","sourceTitle"], # Searches both 'content' and "title" fileds
                                            }
                                }
                        },
                        {
                            "query":{
                                "text_expansion":{
                                "content_embedding":{
                                    "model_id":emdeddingmodel,
                                    "model_text":query_text,
                                                    }
                                                }
                                    }
                        }
                                        ],
                        "rank":{
                            "rrf": {
                                "window_size": 50,
                                "rank_constant": 20
                                    }
                                }
                    },
                size=5,
                from_=0,
                source=True
				)
print(resp)

Which version of Elasticsearch are you using?

I am using '8.11.1'

In fact, both full text search and semantic search queries are working as expected independently. I face error when I combine them using sub_searches.

Are you sending in sub_searches under the query parameter? It should be at the top level, as in this example.

1 Like

@Kathleen_DeRusso Thanks for looking into it.

When i keep sub_searches under top level in my code, i am getting error that Elasticsearch.search() got an unexpected keyword argument 'sub_searches'.

The link that you pointed has CURL code, that works inside Dev Tool Console. But when i am writing python i am missing something minor thing. I searhed on Elastic Websites and open internet for a sample python code, but no luck.
A wokring sample python code for 'sub_searches' would be of great help.

query_text ="CMOS"
index_Name_emb="sales_test"
emdeddingmodel=".elser_model_2"

# Perform the search
resp= client.search(index=index_Name_emb,
                    size=5,
                    from_=0,
                    source=True,
                    sub_searches=[
                        {
                            "query":{
                            "multi_match" : {
                                "query":"CMOS",
                                "fields": ["content","sourceTitle"]
                                }
                                }
                            },
                            {
                                "query":{
                                "text_expansion":{
                                    "content_embedding":{
                                    "model_id":".elser_model_2",
                                    "model_text":"CMOS"
                                    }
                                }
                                }
                            }
                        ],
                        rank={
                            "rrf": {
                                "window_size": 50,
                                "rank_constant": 20
                                    }
                                }

        )
resp

hi there,

sub_searches is still in technical preview and not currently supported by the python lang client. The error you see is being thrown by the client, due to unsupported syntax.

See this doc on how to use sub_searches in python via request. Hybrid Search — Elastic Search Labs

Joe

1 Like

Hi @joemcelroy Thanks for response.

I could get the results when i execute below code inside Kibana Dev Tool console. All good.

GET sales_content_appbriefs_test_dele/_search
{
  "sub_searches":[
    {
      "query":{
        "multi_match" : {
          "query":"CMOS",
          "fields": ["content","sourceTitle_lvl1"]
        }
      }
    },
    {
      "query":{
        "text_expansion":{
          "content_embedding":{
            "model_id":".elser_model_2",
            "model_text":"CMOS"
          }
        }
      }
    }
    ],
    "rank":{
      "rrf": {
        "window_size": 50,
        "rank_constant": 20
      }
    }
}

I formatted same code in Python Lang client.

ConnectionError: Connection error caused by: ConnectionError(Connection error caused by: HostChangedError(HTTPSConnectionPool(host='10.32.25.065', port=9200): Tried to open a foreign host with url: sales_content/_search))

rep=client.perform_request(
            method='GET',
            path=f'sales_content/_search',
            headers={'Content-Type': 'application/json',
                     'Accept': 'application/json'},
            body=json.dumps({
                    'sub_searches':[
                        {
                            "query":{
                            "multi_match" : {
                                "query":"CMOS",
                                "fields": ["content","sourceTitle_lvl1"]
                                }
                                }
                            },
                            {
                                "query":{
                                "text_expansion":{
                                    "content_embedding":{
                                    "model_id":".elser_model_2",
                                    "model_text":"CMOS"
                                    }
                                }
                                }
                            }
                        ],
                        "rank":{
                            "rrf": {
                                "window_size": 50,
                                "rank_constant": 20
                                    }
                                }
                    }),
)
resp

Also there is a statement in the tutorial link that says :
With this version, the sub_searches argument can be used to send multiple search queries as follows:

What is intented to convey by With this version ? What version ? which library ?

Hey,

Might be down to your indexName is incorrect. In your example, you are performing a search to sales_content_appbriefs_test_dele and the python code is sales_content

Python Lang client version should match the Elasticsearch version.

The version is talking specifically that the version of how the query is called (in previous examples we are calling using the search method, now been changed with perform_request. Its not talking about the version of the client.

Joe

Thanks.
Index name i am using is correct. I am trying with different names. Two different names are seen in earlier code and previous code is just due to the face that they come from 2 different trails.

May i know what exactly the Path ? Is this path=f'sales_content/_search', is correct if want to search index name sales_content ?

I got where the problem was; There should have been a back slosh(/) at the begning of the path as below.

path=f'**/**sales_content/_search',

Thanks for all your suggestions.

Any idea on time line by when sub_searches moved from technical review to production release ?

We're still discussing internally. The syntax is currently being discussed and finalised in the next couple of releases I hope.

Joe

Thanks Joe. That’s a useful info.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.