[Python] Highlighting retrieved nested text chunks in elastic search?

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    i have a hybrid search index with a mapping like:

    {
    ...
    "raw_content": {
    "type": "text",
    "term_vector": "with_positions_offsets"
    },
    "text_passages": {
    "type": "nested",
    "properties": {
    "vector": {"type": "dense_vector": "dims": 768},
    "content": {"type":"text", "term_vector": "with_positions_offsets"}
    ...
    }
    }
    ...
    }


    i have a query that uses knn search to get chunks that match some query:

    "knn": {
    "field": "text_passages.vector",
    "query_vector": image_embedding,
    "k": 10,
    "num_candidates": 10 * 3,
    "boost": 1,
    "inner_hits": {
    "name": "matching_passages",
    "size": 2,
    "_source": ["text_passages.content", "text_passages.chunk_num"],
    }
    }


    My hope is to use the highlighter to highlight the two returned inner hits in this query, but for the life of me i cannot figure this out. I've tried placing a highlighter:

    "highlight": {
    "order": "score",
    "fields": {
    "text_passages.content": {
    "type":"unified",
    "fragment_size": 150,
    "number_of_fragments":3 ,
    "pre_tags": ["<strong>"],
    "post_tags": ["</strong>"]
    }
    }
    }


    inside knn, inside inner_hits, outside knn, i cant seem to get this work which is confusing because the docs say they support highlighting in inner_hits. Has anyone done before? am i approaching embeddings wrong, and the text chunks/vectors of a document should be in an array instead of a nested object? very confused. any help is appreciated.

    btw im using elastic 8.17 and python 3.12.7.

    Continue reading...

Compartilhe esta Página