0
votes

I need to write a very complex query in Elastic Search.

The query is stored in a string, query, and the idea is to do a "bool" match on the fields title and content, but then give a special weight to words such as "java" or "python".

My attempt at doing this is

{
    "fields": ["title","content","id"],
     "query": {
       "function_score": {
       "query":
         {
                "bool":
                {
                    "should":
                    [
                        {"match": {"title": {"query": query, "boost": 15}}},
                        {"match": {"content": {"query": query, "boost": 9}}}
                    ]
                }
            }
         },
         "functions": [
            {
            "filter": {
               "match": {
                 "title": "java"
            }},
            "weight": 2 },
            {
            "filter": {
               "match": {
                 "title": "python"
            }},
            "weight": 2 }
        ],
         "boost_mode": "multiply"
       }
} 

But I get an error message saying that the query is malformed. I'm sorry if this is a naive questions, but I don't know where else to turn.

1

1 Answers

0
votes

Try this:

{
  "fields": [
    "title",
    "content",
    "id"
  ],
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "title": {
                  "query": query,
                  "boost": 15
                }
              }
            },
            {
              "match": {
                "content": {
                  "query": query,
                  "boost": 9
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "match": {
              "title": "java"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "match": {
              "title": "python"
            }
          },
          "weight": 2
        }
      ],
      "boost_mode": "multiply"
    }
  }
}