vrepsys
vrepsys

Reputation: 2213

Unable to find a field mapper for field in nested query using field_value_factor

Here's the mapping:

PUT books-index
{
  "mappings": {
    "books": {
      "properties": {
        "tags": {
          "type": "nested",
          "fields": {
            "name": {
              "type": "string"
            },
            "weight": {
              "type": "float"
            }
          }
        }
      }
    }
  }
}

Then doing a nested query using a field_value_factor fails with an error

GET books-index/books/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "score_mode": "sum",
      "query": {
        "function_score": {
          "query": {
            "match": {
              "tags.name": "world"
            }
          },
        "field_value_factor": {
            "field": "weight"
         }
        }
      }
    }
  }
}

The error: "nested: ElasticsearchException[Unable to find a field mapper for field [weight]]"

Interestingly, if there's one book in the index with tags - there's no error and the query works well.

Why is this happening? how can I prevent the error when there are no books with tags in the index?

Any ideas?

Thank you!

P.S. There's also an issue on github for this: https://github.com/elastic/elasticsearch/issues/12871

Upvotes: 1

Views: 1465

Answers (1)

IanGabes
IanGabes

Reputation: 2797

it looks like your mapping is incorrect.

After PUTing the mapping you provided, try executing GET books-index/_mapping, It will show these results:

"books-index": {
  "mappings": {
     "books": {
        "properties": {
           "tags": {
              "type": "nested"
           }
        }
     }
  }
}

It's missing name and weight! The problem with the mapping is that you used either you used fields instead of properties, or you forget to put in a second properties key.

I modified your mapping to reflect that you were looking for a nested name and type within tags, as it looks like that is what your query wants.

PUT books-index
{
  "mappings": {
    "books": {
      "properties": {
        "tags": {
          "type": "nested",
          "properties": {               // <--- HERE!
            "name": {
              "type": "string"
            },
            "weight": {
              "type": "float"
            }
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions