Unordered results from Elastic search

(Sameer Darpe) #1

Good Day!
I am currently using Elastic search 5.2 upgraded from 0.9 followed by 2.3. Right now working on project which displays the opportunities based on Talent segment, location and career level. I am expecting that latest opportunities should be displayed and its depend on "Posted Date" field in opportunities data.
But right now its not happening, its giving unordered data. Below is screenshot for your reference.

Also sharing some code snippet with you.


List<BaseQuery> careerLevelQuery = new List<BaseQuery>();
careerLevelQuery.Add(Nest.Query.CustomScore(cs => cs.Query(t => t.Term(Constants.FilterCareerLevel, careerLevel.ToString())).Script("30")));
careerLevelQuery.Add(Nest.Query.CustomScore(cs => cs.Query(t => t.Term(Constants.FilterCareerLevel, aboveCareerLevel.ToString())).Script("12")));
careerLevelQuery.Add(Nest.Query.CustomScore(cs => cs.Query(t => t.Term(Constants.FilterCareerLevel, belowCareerLevel.ToString())).Script("1")));

List<BaseFilter> mustNotnonGCPFilters = new List<BaseFilter>();
List<BaseFilter> mustClause = new List<BaseFilter>();
mustClause.Add(Nest.Filter.Term("status", "Open"));

_search = _search.Query(fq => fq.Filtered(
q => q.Query(
	qd => qd.Bool(
	 b => b.Must(
		m => m.Bool(bt => bt.Should(talentSegmentQuery.ToArray())),
		m => m.Bool(bl => bl.Should(locationAndCountryQuery.ToArray()).MinimumNumberShouldMatch(1)),
		m => m.Bool(bt => bt.Should(careerLevelQuery.ToArray()))
	.Filter(flt => flt.Bool(
		b => b.Must(mustClause.ToArray())


List<QueryContainer> careerLevelQuery = new List<QueryContainer>();

careerLevelQuery.Add(Query<Position>.FunctionScore(cs => cs.Query(t => t.Term(Constants.FilterCareerLevel, careerLevel.ToString()))
                        .Functions(f => f.ScriptScore(sc => sc.Script(s => s.Inline("30"))))));

careerLevelQuery.Add(Query<Position>.FunctionScore(cs => cs.Query(t => t.Term(Constants.FilterCareerLevel, aboveCareerLevel.ToString()))
                        .Functions(f => f.ScriptScore(sc => sc.Script(s => s.Inline("12"))))));

careerLevelQuery.Add(Query<Position>.FunctionScore(cs => cs.Query(t => t.Term(Constants.FilterCareerLevel, belowCareerLevel.ToString()))
                        .Functions(f => f.ScriptScore(sc => sc.Script(s => s.Inline("1"))))));

SearchDescriptor<Position> _search = new SearchDescriptor<Position>();
_search = _search.Index(string.Join(",", objIndices.ElasticIndices)).Type(string.Join(",", objIndices.ElasticTypes))

List<QueryContainer> mustClause = new List<QueryContainer>();
mustClause.Add(Query<Position>.Bool(x => x.Filter(y => y.Term("status", "Open"))));
List<QueryContainer> mustNotnonGCPFilters = new List<QueryContainer>();

_search = _search.Query(
fq => fq.Bool(
q => q.Must(
	qd => qd.Bool(
		b => b.Must(
			m => m.Bool(bt => bt.Should(talentSegmentQuery.ToArray())),
			m => m.Bool(bl => bl.Should(locationAndCountryQuery.ToArray()).MinimumShouldMatch(1)),
			m => m.Bool(bt => bt.Should(careerLevelQuery.ToArray()))
	.Filter(flt => flt.Bool(
			b => b.Must(mustClause.ToArray())

And Finally...

ISearchResponse<Position> response = new SearchResponse<Position>();
response = client.Search<Position>(_search);

Can you please let me know why this is happening.


(David Pilato) #2

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:


There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

(Sameer Darpe) #3

I updated the post. Seems like more readable. Thanks!

(Sameer Darpe) #4

I think I got my answer after some R&D.

As per description of ES, it makes use of the Lucene scoring formula, which represents the relevance score of each document with a positive floating-point number known as the _score. A query clause generates a _score for each document, and the calculation of that score depends on the type of query clause.

So I created the query and run in Sense console. I observed that the documents which I am getting is according to _score value i.e. descending order of _score value. That's the reason why I am getting unordered results and not according to posted date field.

Happy Coding. :slight_smile:

(system) closed #5

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.