Nest renders range boundaries as string instead of

2019-08-29 23:00发布

I am querying ElasticSearch with Nest, with this code:

var mustTerms = new List<Func<QueryDescriptor<ElasticCheckIn>, Nest.QueryContainer>>();

var dmaxPrice = maxPrice.HasValue ? (double?)maxPrice.Value : 100000d;
var dminPrice = minPrice.HasValue ? (double?)minPrice.Value : 0d;
mustTerms.Add(mt => mt.Range(rd => rd.OnField("price").LowerOrEquals(dmaxPrice).GreaterOrEquals(dminPrice)));

Func<QueryDescriptor<ElasticCheckIn>, Nest.QueryContainer> queryFunc = qd => qd
    .FunctionScore(fsq => fsq
         .Query(fsqd => fsqd
             .Bool(bqd => bqd
                 .Must(mustTerms.ToArray())
                 .Should(shouldTerms.ToArray())))
                 .Functions(fsd => fsd
                     .Linear("createDate", fsdfd => fsdfd
                         .Scale("1d")
                         .Decay(0.5d)
                         .Origin(DateTime.UtcNow.ToString("O")))));

Func<SearchDescriptor<ElasticCheckIn>, SearchDescriptor<ElasticCheckIn>> searchFunc = q => q
    .Index(_indexName)
    .Type(_typeName)
    .Query(queryFunc)
    .Size(limit);

This code produces query:

{
  "query": {
    "function_score": {
      "functions": [
        {
          "linear": {
            "createDate": {
              "origin": "2015-03-16T12:48:14.2660667Z",
              "scale": "1d",
              "decay": 0.5
            }
          }
        }
      ],
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "price": {
                  "gte": "29.97",
                  "lte": "67.5"
                }
              }
            }
          ]
        }
      }
    }
  }
}

As you see, Nest renders the double values in the range part as strings:

{
    "range": {
        "price": {
            "gte": "29.97",
            "lte": "67.5"
        }
    }
}

This results in:

NumberFormatException[For input string: "29.97"]

When I correct the double values by hand, elasticsearch correctly return the query results. How can I get Nest to render the double values correctly?

Update:

Mapping for this index:

{
  "myindice": {
    "mappings": {
      "mytype": {
        "properties": {
          "createDate": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "id": {
            "type": "string"
          },
          /* other fields */
          "price": {
            "type": "long"
          }
          /* other fields */
        }
      }
    }
  }
}

We did not map the price field as long specifically. Some of the price fields on the docs are integers, some doubles.

1条回答
疯言疯语
2楼-- · 2019-08-29 23:33

This may be a bug in NEST using the syntax that you've choosen, you could create a new issue in their github repo. A more standard syntax would be to create a C# POCO to represent your ES data. something like this:

[ElasticType]
public class Type1
{
    public string Id { get; set; }
    public DateTime CreationDate { get; set; }
    public double Price { get; set; }
}

With that, your query syntax become much, much simpler:

         var result2 = ElasticClient.Search<Type1>(s=>s
             .Query(q=> q
                .Filtered(f=>f
                    .Filter(ff=> ff
                        .Range(r=>r
                            .OnField(rf=>rf.Price)
                            .GreaterOrEquals(29.97)
                            .LowerOrEquals(67.5)
                        )
                    )
                )
             )
        );
查看更多
登录 后发表回答