Elasticsearch Search Fields

Viewed 492

I am trying to search for a particular string in two fields. Right I am able to get the desired result. Now I want to rank the result in such a way that if value found in one key, it should be given more priority. I tried doing the following way but got error.

res = es.search(index="pdf_test", body={
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "72141",
                    }
                }
            ]
        }
    },
    "search_fields": {
        "row_data": {
            "weight": 10
        },
        "page_text": {
            "weight": 1
        }
    }
})

I got the follwing error

elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', 'Unknown key for a START_OBJECT in [search_fields].')

I also tried the folllowing query as shown here

res = es.search(index="pdf_test", body={
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "72141",
                        "search_fields": {
                            "row_data": {
                                "weight": 10
                            },
                            "page_text": {
                                "weight": 1
                            }
                        }
                    }
                }
            ]
        }
    }
})

elasticsearch.exceptions.RequestError: RequestError(400, 'x_content_parse_exception', '[multi_match] unknown token [START_OBJECT] after [search_fields]')

1 Answers

You can rank the search result by using "^" in with their names. In your case it would be something like this.

res = es.search(index="pdf_test", body={
    "query": {
        "bool": {
            "must": [
                {
                    "multi_match": {
                        "query": "72141",
                        "fields": ["row_data^10", "page_text^1"]
                    }
                }
            ]
        }
    }
})
Related