1

I have a user profile with some fields like:

  • username
  • gender
  • nationality
  • age
  • language
  • preferred language
  • preferred age

Each user should be able to search other users based on some filters (username, gender, age, language) and the final result should be sorted by a score based on preferences (preferred language, preferred age)

Consider this example:

User profile:

  • username = 'randomdude'
  • gender = 'male'
  • nationality = 'italian'
  • age = '21'
  • language = 'english,italian,spanish'
  • preferred language = 'english,italian'
  • preferred age = '20-30'

Randomdude wants to make a research on our ElasticSearch documents and he would set these filters:

  • gender = 'female'
  • nationality = 'spanish'

So the final list should contain ONLY SPANISH FEMALES but it should prefer people with ENGLISH or ITALIAN in the language field and age between 20 and 30, without excluding the rest. So he still can see people that talk in spanish and have 40, but almost at the end of the list.

I'm using the Elasticsearch's php wrapper. I tried using a lot of functions and I think I should use the function score query but I can't make it work because the documentation is very confusing for me.

1
  • Exactly, function_score is the way to go. Would be nice to share what you tried and where you failed. Actually, the function_score documentation is probably the best piece of docs that we ever had, as some people said on twitter :) twitter.com/alexbilbie/status/382136147893571584 Commented Nov 27, 2013 at 14:52

1 Answer 1

0

I'm going to understand ES :D I solved this issue with a bool query like this but I'm not sure that is the best solution

{
    "query" : {
        "bool" : {
            "must" :[
                {"term" : { "gender" : "female" } },
                {"term" : { "nationality" : "spanish" } },
            ],
            "should" :[
                {"match" : { "language" : { "query" : "english italian","operator" : "or" } } },
            ]
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.