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"
    }
  }
}
Neo-coder
  • 7,715
  • 4
  • 33
  • 52
Daniel Buckle
  • 628
  • 1
  • 9
  • 19

2 Answers2

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": {}
              }
            }
          ]
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
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"

Julien C.
  • 946
  • 8
  • 22
  • 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 – Daniel Buckle May 26 '15 at 13:50
  • 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. – Julien C. May 26 '15 at 18:43