Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8052281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:30:27+00:00 2026-06-05T07:30:27+00:00

I’m indexing documents in Elastic Search that contain arrays. Sample documents : doc1: {

  • 0

I’m indexing documents in Elastic Search that contain arrays.

Sample documents :

doc1:
{
  ...
  actors: ["Tom Cruise", "Brad Pitt", ...],
  ...
}

doc2:
{
  ...
  actors: ["Brad Pitt", "Tom Cruise", ...],
  ...
}

When searching in such documents, I would like to have a score dependent of the matching position in the array, meaning that in the sample documents, searching “Tom Cruise” should boost the first document doc1 because its matching position is 1.

The only solution I can think of right now is by adding a limited number of fields (something like 5) containing the first actors, and putting boosts, like :

doc1:
{
  ...
  actors: ["Tom Cruise", "Brad Pitt", ...],
  actor1: "Tom Cruise",
  actor2: "Brad Pitt",
  ...
}

with actor1 having a boost of 5, actor2 4, and so on.

Do you have a better solution to handle that, maybe using custom_score ?

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:30:28+00:00Added an answer on June 5, 2026 at 7:30 am

    Given this

    curl -XPOST localhost:9200/films
    
    curl -XPOST localhost:9200/films/film/1 -d'{
        actors: ["Tom Cruise", "Brad Pitt", "Patrick Stewart", "Christopher Walken"]
    }'
    curl -XPOST localhost:9200/films/film/2 -d'{
        actors: ["Brad Pitt", "Patrick Stewart", "Tom Cruise", "Christopher Walken"]
    }'
    

    Then this query

    {
        "query":{
            "custom_score":{
                "query": {"match_all":{}},
                "script":"length = _source.actors.size();
                found = false; index=0;
                while(!found && index<length){
                  if(_source.actors[index] == target){
                    found=true;
                  }
                  else{
                    index+=1
                  }
                }
                length - index;",
                "params":{
                    "target": "Tom Cruise"
                }
            }
        }
    }
    

    calculates a score of 4 for the first film and 2 for the last one (if you’re pasting this into curl, I had to remove all the line breaks in the custom script)

    Some caveats:

    • you probably want a better way of converting offset to score: this code returns the length - offset as the score so you can only really compare things of the same length
    • it looks like doc.actors (i.e. the indexed data) only has an alphabetically sorted version of the array, which is obviously not useful, so I had to use _source which I believe is a lot slower. It might be acceptable performance-wise if the custom_score query wraps a filtered query.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.