3

I've been trying to run the following query, but every time I run it I receive the following error:

nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"field_value_factor\"]; }]","status":400

Here is the query:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [{
            "match": {
              "thread_name": "parenting"
            }
          }, {
            "nested": {
              "path": "messages",
              "query": {
                "bool": {
                  "should": [{
                    "match": {
                      "messages.message_text": "parenting"
                    }
                  }]
                }
              },
              "inner_hits": {}
            }
          }]
        }
      }
    },
    "field_value_factor": {
      "field": "thread_view"
    }
  }
}

2 Answers 2

2

Your field_value_factor function is misplaced. it should be nested within the functions property. Try this query instead

{
  "query": {
    "function_score": {
      "functions": [
        {
          "field_value_factor": {
            "field": "thread_view"
          }
        }
      ],
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "thread_name": "parenting"
              }
            },
            {
              "nested": {
                "path": "messages",
                "query": {
                  "bool": {
                    "should": [
                      {
                        "match": {
                          "messages.message_text": "parenting"
                        }
                      }
                    ]
                  }
                },
                "inner_hits": {}
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have an error in your query, field_value_factor is n attribute of function_score:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [{
            "match": {
              "thread_name": "parenting"
            }
          }, {
            "nested": {
              "path": "messages",
              "query": {
                "bool": {
                  "should": [{
                    "match": {
                      "messages.message_text": "parenting"
                    }
                  }]
                }
              },
              "inner_hits": {}
            }
          }]
        }
      },
      "field_value_factor": {
        "field": "thread_view"
      }
    }
  }
}

since you have a single function, you don't need to nest this into "functions"

2 Comments

Ah thank you so much! Its now not giving any errors, but is there any way to separate the thread_name results and the message_text results. Once I put in the function score the results are mixed, returning message_text first because its more popular than a result with the keyword in the thread title
no you can't do such thing in a single query, but you could use a boost on one of the field to ensure results for this field will always be first.

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.