public void PASS_Serialize()
        {
            FunctionScoreQuery query = new FunctionScoreQuery(
                new TermQuery("field", "value"),
                new List<ScoreFunctionBase>() { new RandomScoreFunction(11111) });

            string json = JsonConvert.SerializeObject(query);

            Assert.IsNotNull(json);

            string expectedJson = "{\"function_score\":{\"query\":{\"term\":{\"field\":\"value\"}},\"random_score\":{\"seed\":11111}}}";
            Assert.AreEqual(expectedJson, json);
        }
 public void FAIL_CreateQuery_Query()
 {
     try
     {
         FunctionScoreQuery query = new FunctionScoreQuery(
         null,
         new List<ScoreFunctionBase>() { new RandomScoreFunction(11111) });
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("searchPiece", argEx.ParamName);
     }
 }
 public void FAIL_CreateQuery_ScoreFunction()
 {
     try
     {
         FunctionScoreQuery query = new FunctionScoreQuery(
         new TermQuery("field", "value"),
         null);
         Assert.Fail();
     }
     catch (ArgumentNullException argEx)
     {
         Assert.AreEqual("scoreFunctions", argEx.ParamName);
     }
 }
        public void PASS_CreateQuery()
        {
            FunctionScoreQuery query = new FunctionScoreQuery(
                new TermQuery("field", "value"),
                new List<ScoreFunctionBase>() { new RandomScoreFunction(11111) });

            Assert.IsNotNull(query);
            Assert.IsTrue(query.Query is TermQuery);
            Assert.AreEqual("field", (query.Query as TermQuery).Field);
            Assert.AreEqual("value", (query.Query as TermQuery).Value.ToString());
            Assert.AreEqual(1, query.ScoreFunctions.Count());
            Assert.IsTrue(query.ScoreFunctions.First() is RandomScoreFunction);
            Assert.AreEqual(11111, (query.ScoreFunctions.First() as RandomScoreFunction).Seed);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Dictionary<string, object> scoreDict = serializer.Deserialize<Dictionary<string, object>>(reader);
            if (scoreDict.ContainsKey(_FUNCTION_SCORE))
                scoreDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(scoreDict.GetString(_FUNCTION_SCORE));

            List<ScoreFunctionBase> functionList = new List<ScoreFunctionBase>();
            if(scoreDict.ContainsKey(_FUNCTIONS))
            {
                List<Dictionary<string, object>> functionsDictList = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(scoreDict.GetString(_FUNCTIONS));
                
                foreach(Dictionary<string, object> functionDict in functionsDictList)
                {
                    IFilter filter = null;
                    if(functionDict.ContainsKey(_FILTER))
                        filter = JsonConvert.DeserializeObject<IFilter>(functionDict.GetString(_FILTER));

                    ScoreFunctionEnum functionEnum = ScoreFunctionEnum.BoostFactor;
                    string scoreFunctionKey = functionDict.Keys.FirstOrDefault(x => ScoreFunctionEnum.Find(x) != null);
                    ScoreFunctionEnum scoreFunctionType = ScoreFunctionEnum.Find(scoreFunctionKey);
                    if(string.IsNullOrWhiteSpace(scoreFunctionKey))
                        throw new Exception("Unable to find a score function key.");

                    ScoreFunctionBase functionBase = JsonConvert.DeserializeObject(functionDict[scoreFunctionKey].ToString(), scoreFunctionType.ImplementationType) as ScoreFunctionBase;

                    functionBase.Filter = filter;

                    functionList.Add(functionBase);
                }
            }
            else
            {
                string scoreFunctionKey = scoreDict.Keys.FirstOrDefault(x => ScoreFunctionEnum.Find(x) != null);
                ScoreFunctionEnum scoreFunctionType = ScoreFunctionEnum.Find(scoreFunctionKey);
                if(string.IsNullOrWhiteSpace(scoreFunctionKey))
                    throw new Exception("Unable to find a score function key.");

                ScoreFunctionBase functionBase = JsonConvert.DeserializeObject(scoreDict.GetString(scoreFunctionKey), scoreFunctionType.ImplementationType) as ScoreFunctionBase;

                functionList.Add(functionBase);
            }

            FunctionScoreQueryBase functionScore = null;

            if (scoreDict.ContainsKey(_QUERY))
            {
                IQuery query = JsonConvert.DeserializeObject<IQuery>(scoreDict.GetString(_QUERY));
                functionScore = new FunctionScoreQuery(query, functionList);
            }
            else
            {
                IFilter filter = JsonConvert.DeserializeObject<IFilter>(scoreDict.GetString(_FILTER));
                functionScore = new FunctionScoreFilter(filter, functionList);
            }

            functionScore.Boost = scoreDict.GetDouble(QuerySerializer._BOOST, QuerySerializer._BOOST_DEFAULT);
            functionScore.MaximumBoost = scoreDict.GetDouble(_MAX_BOOST, _MAX_BOOST_DEFAULT);
            functionScore.BoostMode = BoostModeEnum.Find(scoreDict.GetString(_BOOST_MODE, _BOOST_MODE_DEFAULT.ToString()));
            functionScore.ScoreMode = ScoreModeEnum.Find(scoreDict.GetString(_SCORE_MODE, _SCORE_MODE_DEFAULT.ToString()));
            functionScore.QueryName = scoreDict.GetStringOrDefault(QuerySerializer._QUERY_NAME);
            return functionScore;
        }