Issue with script accessing nested field

I work at a ecommerce company and we are rebuilding our search engine.
One of our queries is to discover what kind of category a given user query represents, like android should return smartphones category.

But we don't have a category android, but real users when search for android usually clicks on products of smartphones category, so we built a app to track this clicks and we index along the category the most hitted queries.

This is the mapping:

{
  "category": {
    "properties": {
      "name": {
        "type": "string",
        "fields": {
          
        }
      },
      "id": {
        "type": "long"
      },
      "queries": {
        "type": "nested",
        "properties": {
          "query": {
            "type": "string",
            "fields": {
              
            }
          },
          "count": {
            "type": "long"
          }
        }
      }
    }
  }
}

And i index documents like this:

{
  "id": 1,
  "name": "Smartphones",
  "queries": [
    {
      "query": "android",
      "count": 100
    },
    {
      "query": "iphone",
      "count": 30
    },
    {
      "query": "moto g",
      "count": 20
    }
  ]
}

And i want to query they by name and also by the user query (there is only the user query on this es query):

{
  "_source": [
    "name",
    "queries"
  ],
  "query": {
    "function_score": {
      "query": {
        "nested": {
          "path": "queries",
          "query": {
            "match": {
              "queries.query": {
                "query": "moto g",
                "type": "phrase"
              }
            }
          }
        }
      },
      "functions": [
        {
          "script_score": {
            "script": "_score * (doc['queries.count'].value * 1.0)"
          }
        }
      ],
      "boost_mode": "replace",
      "score_mode": "max"
    }
  }
}

The problem is, don't know why, but the queries.count seems to be always 0, i need to use nested inside the filter to get the actual document hitted?

Just want to query for moto g and return the smartphones category.

Also we are using Elasticsearch 2.4.4.

Have you considered running these sorts of queries against raw click data (query+clickedProduct+Category docs) rather than trying to roll all that behavioural information up into product documents? I demonstrated that approach at elasticon last year [1] using some click data and outlined some of the benefits of that approach.

[1] https://www.elastic.co/elasticon/conf/2016/sf/graph-capabilities-in-the-elastic-stack (28 minutes into the presentation)

1 Like

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