Python client multisearch with different fields weights

I'm quite new to elasticsearch...
I created my first index (to be used for italian language), and basic search functionality, for my cooking recipes database.

I now am trying to implement some more advanced search features, with multi-field search, giving different weights to different fields.

I use the Python ES client [elasticsearch-py.readthedocs.io, v8.12.1].

This is what I tried, without good results (titles seem to count, other fields do not...):

     query = {
        "multi_match": {
          "query": myQueryTerms,
          "fields": [
            "title^3", "ingredients^2", "descriptionSections", "category",
          ]
        }
      }

Can anybody help?

Unfortunately I find Python client for Elasticsearch API docs quite confusing, and difficult to browse...

What is the mapping of the different fields you are searching? What does a sample query look like? How many matches are returned for this query? The default size is 10, so if you are getting 10 results returned it is possible there are other matches in addition to that.

If the result size is not an issue, can you show a document you would expect to match that does not?

Sorry for being short on details...

Mappings are:

{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "italian_full",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "language": {
      "type": "keyword"
    },
    "provider": {
      "type": "keyword"
    },
    "descriptionSections": {
      "properties": {
        "contents": {
          "type": "text",
          "analyzer": "italian_full"
        },
        "imageName": {
          "enabled": False
        },
        "imageUrl": {
          "enabled": False
        },
        "section": {
          "enabled": False
        }
      }
    },
    "category": {
      "type": "text",
      "analyzer": "italian_full",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 32
        }
      }
    },
    "ingredients": {
      "properties": {
        "title": {
          "enabled": False
        },
        "name": {
          "type": "text",
          "analyzer": "italian_full",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "unit": {
          "enabled": False
        }
      }
    },
    "imageUrl": {
      "enabled": False
    },
    "presentation": {
      "type": "text",
      "analyzer": "italian_full"
    },
    "info": {
      "properties": {
        "cookingTimeMinutes": {
          "type": "short"
        },
        "cost": {
          "type": "keyword"
        },
        "difficulty": {
          "enabled": False
        },
        "dosesForPersons": {
          "enabled": False
        },
        "note": {
          "type": "text",
          "analyzer": "italian_full"
        },
        "other": {
          "type": "text",
          "analyzer": "italian_full"
        },
        "preparationTimeMinutes": {
          "type": "text"
        }
      }
    },
    "link": {
      "type": "keyword"
    },
    "pageName": {
      "type": "keyword"
    }
  }
}

A sample query looks like this one:

     query = {
        "multi_match": {
          "query": "zucchero",
          "fields": [
            "title^3", "ingredients^2", "descriptionSections", "category",
          ]
        }
      }

      arguments = {}
      arguments["index"] = "recipes_it"
      arguments["query"] = query
      arguments["from_"] = 0
      arguments["size"] = 20

      retval = self._es.search(**arguments)

Search results are 0, when I search an ingredient name.
If I search for a term in title, I get the correct number of results.

Should the list of fields queried not be "title^3", "ingredients.name^2", "descriptionSections.contents", "category"... so you actually query the fields that are mapped as text?

Thanks! I did non know about that query usage... I did expect indicating an objecgt in the query would match the whole objecgt fields... Thanks again!
The query correctly returns all the documents with that ingredient now.

One more suggestion: the only source of documentation for ES Python Client API is this one: "https://elasticsearch-py.readthedocs.io/en/v8.12.1/", correct?

As not that familiar with the Python clinet so do not know. When it comes to determining the usage of different parameters I would recommend you also look at the general docs for that query type.

1 Like

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