jeudi 5 novembre 2015

How to use random method in elasticsearch?

I have been trying to implement random function in elastic search. This is the result i got without using random method.

  {
  "took": 5,
  "timed_out": false,
  "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
  },
  "hits": {
     "total": 5,
     "max_score": 0.589526,
     "hits": [
     {
        "_index": "project",
        "_type": "recipes",
        "_id": "74",
        "_score": 0.589526,
        "_source": {
           "title": "A Contribution To Knowledge",
           "description": "no-description",
           "source_name": "Oh She Glows",
        }
     },
    {
        "_index": "project",
        "_type": "recipes",
        "_id": "269",
        "_score": 0.589526,
        "_source": {
           "title": "Loaded Lunchtime Oatmeal",
           "description": "no-description",
           "source_name": "Oh She Glows",
        }
     },
     {
        "_index": "project",
        "_type": "recipes",
        "_id": "238",
        "_score": 0.589526,
        "_source": {
           "title": "Carrot Overload",
           "description": "no-description",
           "source_name": "Oh She Glows",
     }
     },
     {
     "_index": "project",
     "_type": "recipes",
     "_id": "308",
     "_score": 0.589526,
     "_source": {
        "title": "Curried French Lentils",
        "description": "no-description",
        "source_name": "Straight Up Food",
        }
     },
     {
     "_index": "project",
     "_type": "recipes",
     "_id": "135",
     "_score": 0.5792727,
     "_source": {
        "title": "Lentil Rice Casserole",
        "description": "no-description",
        "source_name": "Ken's kitchen",
        }
     }
     ]
     }
  }

After following the random method docs, I wanted to implement random result based on "source_name". Basically, the source_name should not repeat one after the other, it can have a random order of appearance. This is my approach:

 {
"query": {
    "bool": {
        "must": [
            {
                "terms": {
                    "meta_groups": [
                        "vegan"
                    ],
                    "minimum_should_match": 1
                }
            }
        ]
    },
    "function_score": {
      "query": {},
      "functions": [
        {"random_score": {
          "seed": 314159265359
        }}
      ]
    }
  }
 }

But after using the random method, i got this error. Thanks in advance.

"error": "SearchPhaseExecutionException[Failed to execute phase    [query], all shards failed; shardFailures {[DZw6yV3kTsSqka2F9x4mJg][foodini][0]: SearchParseException[[foodini][0]: query[+((meta_groups:vegan)~1)],from[-1],size[-1]: Parse Failure [Failed to parse source [{\n    \"query\": {\n        \"bool\": {\n            \"must\": [\n                {\n                    \"terms\": {\n                        \"meta_groups\": [\n                            \"vegan\"\n                        ],\n                        \"minimum_should_match\": 1\n                    }\n                }\n            ]\n        },\n        \"function_score\": {\n          \"query\": {},\n          \"functions\": [\n            {\"random_score\": {\n              \"seed\": 314159265359\n            




Aucun commentaire:

Enregistrer un commentaire