Adding a specific score if has field with a specific value (nested)

Hello

I have document with nested documents:

{
  "_id": 111
  "price": 1000
  "properties": [
    { "property_id": 22, "property_value": "gas"}
    { "property_id": 23, "property_value": "green"}
  ]
}

I have a request that gives me the expected score by the price (from 1 to 100).
I need to add a +30 score if the nested document has a param_id == 23 AND param_value == "red". However, if the document doesn't have this nested document, then still return the document, just without these +30 score (can't just add filter that will delete the result without specific value)

I try:

{
  "query": {
    "nested": {
      "path": "properties",
      "query": {
          "function_score": {
            "query": {
              "bool": {           // search specific nested document
                "filter": [
                  { "term": { "properties.property_id": 23 } },
                  { "term": { "properties.property_value": "red" } }
                ]
              }
        },
        "script_score": {
          "script": {
            "source": "30"    // if find nested document, then add 30 score
          }
        }
      }
    }
}
  }
  },
  "sort": {
"_score": {
  "order": "desc"
}
  }
}

result: search_phase_execution_exception

I try:

{
  "query": {
    "bool": {
      "must": [
        [
          {
            "nested": {
              "path": "properties",
              "query": [
                {
                  "function_score": {
                    "functions": [
                      {
                       "weight": 30   // if find nested document, then add 30 score
                        "filter": [    // search specific nested document
                            {
                              "term": {
                                "properties.property_id": 23
                              }
                            },
                            {
                              "term": {
                                "properties.property_value": "red"
                              }
                            }
                        ]
                      }
                    ]
                  }
                }
              ]
            }
          }
        ]
      ]
    }
  },
  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}

result: [_na] query malformed, must start with start_object

I try:

{
  "query": {
    "bool": {
      "must": [
        [
          {
            "nested": {
              "path": "properties",
              "query": [
                {
                  "constant_score": {
                    "filter": [  // search specific nested document
                      {
                        "term": {
                          "properties.property_id": 23
                        }
                      },
                      {
                        "term": {
                          "properties.property_value": "red"
                        }
                      }
                    ],
                    "boost": 30  // if find nested document, then add 30 score
                  }
                }
              ]
            }
          }
        ]
      ]
    }
  },
  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}

result: return document where has property_id == 23 AND property_value == "red", but add 1 score, and no dependence on boost

I'm have solution by Igor Motov:

PUT test
{
  "mappings": {
    "doc": {
      "properties": {
        "properties": {
          "type": "nested",
          "properties": {
            "property_id": {
              "type": "integer"
            },
            "property_value": {
              "type": "keyword"
            }
          }
          
        },
        "price": {
          "type": "integer"
        }
      }
    }
  }  
}

PUT test/doc/111
{
  "price": 1000,
  "properties": [
    { "property_id": 22, "property_value": "gas"},
    { "property_id": 23, "property_value": "green"}
  ]
}

PUT test/doc/112
{
  "price": 900,
  "properties": [
    { "property_id": 12, "property_value": "whater"},
    { "property_id": 23, "property_value": "red"}
  ]
}

GET test/doc/_search
{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "script_score": {
            "script": {
              "source": "30"
            }
          },
          "filter": {
            "nested": {
              "path": "properties",
              "query": {
                "bool": {
                  "filter": [
                    {
                      "term": {
                        "properties.property_id": 23
                      }
                    },
                    {
                      "term": {
                        "properties.property_value": "red"
                      }
                    }
                  ]
                }
              }
            }
          }
        },
        {
          "weight": 0
        }
      ],
      "boost_mode": "sum",
      "score_mode": "first"
    }
  },
  "sort": {
    "_score": {
      "order": "desc"
    }
  }
}

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