Query Across Indices and Apply Different Function Scores (get parsing exception unknown field error)


(Calvin Pritchard) #1

I would like to implement a search across indexes that applies different function scoring for each index so that more recent jobs and events rank higher in search results.

Suppose the website has jobs, events given by mappings below.

DELETE event
DELETE job
PUT event
{
  "mappings": {
    "core_job": {
      "properties": {
        "date_created": {
          "type": "date",
          "include_in_all": false
        },
        "start_date": {
          "type": "date",
          "include_in_all": false
        },
        "title": {
          "type": "text",
          "include_in_all": true
        }
      }
    }
  }
}

PUT job
{
  "mappings": {
    "core_job": {
      "properties": {
        "date_created": {
          "type": "date",
          "include_in_all": false
        },
        "title": {
          "type": "text",
          "include_in_all": true
        }
      }
    }
  }
}

PUT event/core_event/core_event:1
{
  "date_created": "2013-05-02T16:23:55+00:00",
  "start_date": "2013-07-02T16:23:55+00:00",
  "title": "Ecology Event"
}

PUT job/core_job/core_job:1
{
  "date_created": "2014-05-02T16:23:55+00:00",
  "title": "Ecology Job"
}

When I try this query

GET event,job/_search
GET event,job/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "function_score": {
            "query": {
              "bool": {
                "must": [
                  {
                    "type": {
                      "value": "core_job"
                    }
                  },
                  {
                    "match": {
                      "_all": "ecology"
                    }
                  }
                ]
              }              
            },
            "functions": [
              {
                "filter": {
                  "type": {
                    "value": "core_job"
                  }
                }, 
                "gauss": {
                  "date_created": {
                    "scale": "10d"
                  }
                }
              }
            ]
          }
        },
        {
          "function_score": {
            "query": {
              "bool": {
                "must": [
                  {
                    "type": {
                      "value": "core_job"
                    }
                  },
                  {
                    "match": {
                      "_all": "ecology"
                    }
                  }
                ]
              }              
            },
            "functions": [
              {
                 "filter": {
                   "type": {
                     "value": "core_job"
                   }
                }, 
                "gauss": {
                  "date_created": {
                    "scale": "10d"
                  }
                }
              },
              {
                "script_score": {
                  "script": "1"
                }
              }
            ],
            "score_mode": "sum"
          }
        },
        {
          "function_score": {
            "query": {
              "bool": {
                "must": [
                  {
                    "type": {
                      "value": "core_event"
                    }
                  }
                ]
              }
            },
            "functions": [
              {
                "filter": {
                  "type": {
                    "value": "core_event"
                  }
                }, 
                "gauss": {
                  "start_date": {
                    "scale": "10d"
                  }
                }
              },
              {
                "script_score": {
                  "script": "1"
                }
              }
            ],
            "score_mode": "sum"
          }
        }
      ]
    }
  }
}

I get a response of

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 10,
    "successful": 5,
    "skipped": 0,
    "failed": 5,
    "failures": [
      {
        "shard": 0,
        "index": "job",
        "node": "Dinaz4RTSceo1xZ2_PX70Q",
        "reason": {
          "type": "parsing_exception",
          "reason": "unknown field [start_date]",
          "line": 1,
          "col": 0
        }
      }
    ]
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "event",
        "_type": "core_event",
        "_id": "core_event:1",
        "_score": 1,
        "_source": {
          "date_created": "2013-05-02T16:23:55+00:00",
          "start_date": "2013-07-02T16:23:55+00:00",
          "title": "Ecology Event"
        }
      }
    ]
  }
}

How should the query be changed to that ElasticSearch does not try to search for start_date on the job index (or is there a better approach to score differently for each index)?

I'm using ElasticSearch 5.6.6 (since I'm using Wagtail)


(system) #2

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