Вопрос

I'm using PyES library for quering the elastcsearch. Let's imagine that my query looks like:

query = MatchAllQuery()
query = query.search(
    fields=[
        "content.title",
        "content.description",
        "content.timestamp",
        "source.name",
        "source.url"
    ],
    count=10
)

result = es_conn.search(
             query=query,
             indices=['my'],
             sort="content.timestamp:desc"
         )

Every result's item is a dict with fields' names as keys, so item = {"content.title": "bla bla", "content.description": "bla bla bla", ... }

My script is only a getter and need to save the results for 3rd party script without processing, but that script requires special keys names: item = { "name": "bla bla", "text": "bla bla bla", ...}

Is it a way to specify in PyES request a rule for renaming fields' names (to "name", "title", "date" etc.) in the returned object?

Of course, i can do that after i got response from elsticsearch by it requires to iterate through the result object (that i what to avoid) and doesn't look so optimal if i have thousand items in the response.

Это было полезно?

Решение

You need to use partial fields, not sure if you can use them with pyes though. I had a quick look at the documentation but couldn't find it.

You JSON query would look like this:

{
    "query" : {
        "match_all" : {}
    },
    "partial_fields" : {
        "name" : {
            "include" : "content.title",
        },
        "text" : {
            "include" : "content.description",
        }
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top