简体   繁体   中英

Nest search query returning different results from the Elastic query DSL

I have an ElasticSearch index called challenges , which contains objects of type Challenge .

When I execute the following filter query in the Kibana console, it returns the 9 results, which are correct.

GET challenges/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "type": "Orphan"
          }
        }
      ]
    }
  }
}

However, the following query from the Nest client, returns zero hits:

var challenges = await _client.SearchAsync<Challenge>(s => s
  .Query(q => +q
    .Term(t => t.Type, Models.Enums.ChallengeType.Orphan)
  )
);

I've also tried the following variation, to no avail:

var challenges = await _client.SearchAsync<Challenge>(s => s
  .Query(q => q
    .Bool(b => b
      .Filter(f => f
        .Term(t => t
          .Field(f => f.Type)
          .Value(challengeType)
        )
      )
    )
  )
);

The type property on which I'm filtering, is an enum with the following values:

public enum ChallengeType
{
  SixDimensions,
  Intro,
  Normal,
  UserCreated,
  Orphan,
  Youmate
}

and is stored as a keyword in the index. An example object that is actually in the index:

{
  "id": "3bce0ce1-9676-4858-b165-1442a443bf5a",
  "icon": "water-bottle.png",
  "index": 0,
  "default-time": "09:00",
  "default-days": [
    "Saturday",
    "Monday",
    "Wednesday"
  ],
  "default-repetitions": 3,
  "category": "A",
  "title": {
    "Persian": "آب خوردن"
  },
  "dimension": "Physical",
  "type": "Orphan",
  "id-package": "00000000-0000-0000-0000-000000000000",
  "intro-pages": [ ],
  "date-created": "2020-10-14T12:39:21.8427517+03:30",
  "notify": true,
  "template": 0,
  "belongs-to-user": "00000000-0000-0000-0000-000000000000",
  "active": false
}

Do you have any suggestions as to why the results from the console differ from when it is executed from the Nest client?

The enum should be marked with StringEnumAttribute to serialize as a string

[StringEnum]
public enum ChallengeType
{
    SixDimensions,
    Intro,
    Normal,
    UserCreated,
    Orphan,
    Youmate
}

Without this, the enum will be serialized as its underlying integer value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM