Skip to content
TheChatArchiveThe Chat Archive
Metallama-4-maverick-17b-128e-instruct· MetaHugging Face LMArena Human Preference 140K

How to request OpenSeaech to rank results regarding to similarity of textual document…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-2025-04-16’s answer.
  1. User

    How to request OpenSeaech to rank results regarding to similarity of textual document field to a value while the documents ids are specified in the query

  2. Meta Assistant

    To request OpenSearch to rank results based on the similarity of a textual document field to a specific "value" while also specifying the document IDs in the query, you can combine a terms query (or ids query for more efficiency when dealing with IDs) with a script-based scoring or a match/more_like_this query for the similarity aspect. However, directly combining these in a way that the similarity score is the primary ranking factor while still filtering by specific IDs requires some creativity.

    Using a bool Query with filter and should Clauses

    One straightforward approach is to use a bool query. You can specify the document IDs using a terms (or ids) query within the filter clause, ensuring that only those documents are considered. For the ranking, you can use a match or more_like_this query within the should clause. The should clause doesn't affect the documents that are included (since they're filtered by the filter clause), but it influences their scoring.

    Here's an example using a match query for similarity:

    {
      "query": {
        "bool": {
          "filter": {
            "ids": {
              "values": ["doc_id_1", "doc_id_2", "doc_id_3"] // Specify the document IDs here
            }
          },
          "should": {
            "match": {
              "document_field": "your similarity value here" // The "value" to compare against
            }
          }
        }
      }
    }
    

    And here's an example using more_like_this:

    {
      "query": {
        "bool": {
          "filter": {
            "ids": {
              "values": ["doc_id_1", "doc_id_2", "doc_id_3"]
            }
          },
          "should": {
            "more_like_this": {
              "fields": ["document_field"],
              "like": "your similarity value here",
              "min_term_freq": 1,
              "max_query_terms": 12
            }
          }
        }
      }
    }
    

    Using a script_score Query

    For more control over the scoring, you can use a script_score query within a function_score query. This allows you to directly compute a score based on a custom script. You can filter the documents using a filter clause and then apply a script to score them based on their similarity to the "value".

    Here's a simplified example:

    {
      "query": {
        "function_score": {
          "query": {
            "ids": {
              "values": ["doc_id_1", "doc_id_2", "doc_id_3"]
            }
          },
          "script_score": {
            "script": {
              "source": "cosineSimilarity(params.query_vector, 'document_embedding') + 1.0",
              "params": {
                "query_vector": [0.1, 0.5, ..., 0.9] // Pre-computed vector for "your similarity value here"
              }
            }
          }
        }
      }
    }
    

    In this example, document_embedding is a dense_vector field in your documents, and you've pre-computed the vector for "your similarity value here". The cosineSimilarity function computes the similarity between the document's vector and the query vector.

    Considerations

    1. Performance: When dealing with a large number of document IDs or complex queries, performance can become a concern. Ensure that your OpenSearch cluster is adequately sized and configured.
    2. Relevance Tuning: You may need to experiment with different query structures, similarity measures, or script scoring functions to achieve the desired relevance ranking.
    3. Vector Search: If you're using vector embeddings, consider the trade-offs between accuracy, indexing time, and query performance when choosing parameters like the number of nearest neighbors (k) or the vector dimensions.

    By combining filtering with scoring techniques, you can effectively rank a specified set of documents based on their similarity to a given "value" in OpenSearch.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.