0

I have some document in elasticsearch index. Here is the sample document

DOC1

{
"feild1":["hi","hello","goodmorning"]
"feild2":"some string"
"feild3":{}
}

DOC2

{
"feild1":["hi","goodmorning"]
"feild2":"some string"
"feild3":{}
}

DOC3

{
"feild1":["hi","hello"]
"feild2":"some string"
"feild3":{}
}

I want to query for feild1 having values "hi" and "hello" if both is present then that document should come first if any one is present then it should come after that. for example: result should be in order of DOC1, DOC3, DOC2. I tried with boost query. but it is retuning not in the order that I want. Here is the query that I am trying.

{
    "query": {
        "bool": {
            "must": [
                {
                    "match_phrase": {
                       "avail_status": true
                    }
                },
               {
                   "bool": {
                       "should": [
                          {
                               "constant_score": {
                                  "filter": {
                                  "terms": {
                                     "feild1": [
                                        "hi"
                                     ]
                                  }
                                  },
                                  "boost": 20
                               }
                           },
                           {
                               "constant_score": {
                                  "filter": {
                                  "terms": {
                                     "feild1": [
                                        "hello"
                                     ]
                                  }
                                  },
                                  "boost": 18
                               }
                           }
                       ]
                   }
               }
            ]
        }
    }
}

this is returning me first those document having "hi" and then those having "hello". Thanks in advance!

sehe
  • 374,641
  • 47
  • 450
  • 633
nishant kumar
  • 507
  • 10
  • 28
  • with the current query if you can check you can see doc3 and doc1 got equal score since wrapping filters inside custom_score igonore TDF/IDF. I don't see a point for ES to put doc3 above doc1. If you are exerting a pattern so as place array with more fields above the array with less fields along with boosting for matched values then i suggest start looking at function score. let me know if this the case for you – user3775217 Jun 06 '17 at 10:37
  • yes I am looking for that only, document have high matching score – nishant kumar Jun 06 '17 at 10:53
  • i posted for funtion score – user3775217 Jun 07 '17 at 06:58

1 Answers1

2

To add extra boost for documents with larger field1, you can put funtion_score script score.

Mappings

{
  "mappings": {
    "document_type" : {
      "properties": {
        "field1" : {
          "type": "text",
          "fielddata": true
        },
        "field2" : {
          "type": "text"
        },
        "field3" : {
          "type": "text"
        }
      }
    }
  }
}

Index documents

POST custom_score_index1/document_type

{
"feild1":["hi","hello","goodmorning"],
"feild2":"some string",
"feild3":{}
}

POST custom_score_index1/document_type

{
"feild1":["hi","goodmorning"],
"feild2":"some string",
"feild3":{}
}

POST custom_score_index1/document_type

{
"feild1":["hi","hello"],
"feild2":"some string",
"feild3":{}
}

Query with function score add extra _score for larger size for field1

POST custom_score_index1/document_type/_search
{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [{
                            "match_phrase": {
                                "avail_status": true
                            }
                        },
                        {
                            "bool": {
                                "should": [{
                                        "constant_score": {
                                            "filter": {
                                                "terms": {
                                                    "feild1": [
                                                        "hi"
                                                    ]
                                                }
                                            },
                                            "boost": 20
                                        }
                                    },
                                    {
                                        "constant_score": {
                                            "filter": {
                                                "terms": {
                                                    "feild1": [
                                                        "hello"
                                                    ]
                                                }
                                            },
                                            "boost": 18
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            "functions": [{
                "script_score": {
                    "script": {
                        "inline": "_score + 10000 * doc['field1'].length"
                    }
                }
            }],
            "score_mode": "sum",
            "boost_mode": "sum"
        }
    }
}
user3775217
  • 4,675
  • 1
  • 22
  • 33