在c#中的ElasticSearch中编写两个应条件

时间:2018-07-23 17:02:39

标签: c# elasticsearch elasticsearch-5 elasticsearch-plugin sense

我写了一个ElasticSearch感应查询,但是我想用c#MVC编写相同的查询,但是我没有从c#查询中获取实际结果,而是从感应查询中获取了正确的数据。 有人可以帮我吗?

下面我要发布两个查询:

1> ElasticSearch意义查询

GET data*/data/_search
{
  "query": {
    "bool": {
      "should": [{
          "bool": {
            "must": [{
                "match_phrase": {
                  "message": "Test1"
                }
              },
              {
                "match_phrase": {
                  "message": "Test2"
                }
              }
            ],
            "must_not": [{
              "match_phrase": {
                "message": "Account: Test3"
              }
            }]
          }
        },
        {
          "bool": {
            "must": [{
              "match_phrase": {
                "message": "User1"
              }
            }],
            "should": [{
                "match_phrase": {
                  "message": "User2"
                }
              },
              {
                "match_phrase": {
                  "message": "User3"
                }
              },
              {
                "match_phrase": {
                  "message": "User4"
                }
              }
            ],
            "minimum_number_should_match": 1
          }
        }
      ],
      "minimum_number_should_match": 1,
      "must": [{
        "range": {
          "@timestamp": {
            "from": "2018-07-22T18:30:00Z",
            "to": "2018-07-23T18:29:59Z"
          }
        }
      }]
    }
  },
  "size": 99,
  "sort": [{
    "@timestamp": {
      "order": "desc"
    }
  }]
}

2>用于c#查询

var Results = call.Search<Data>(s => s.Type(data)
    .Sort(k => k.Descending(m => m.timestamp))
    .Size(100)
    .Query(q => q
       .Bool(b => b
         .Should(ss => ss
          .Bool(ssb => ssb
            .Must(
                subQuery => subQuery.MatchPhrase(n => n.Field(p => p.message).Query("Test1")),
                subQuery => subQuery.MatchPhrase(n => n.Field(p => p.message).Query("Test2"))
                )
                .MustNot(subQuery => subQuery.MatchPhrase(n => n.Field(p => p.message).Query("Account: Test3")))))
          .Must(subQuery => subQuery.MatchPhrase(n => n.Field(p => p.message).Query("User1"))
         ).Should(
            subQuery => subQuery.MatchPhrase(n => n.Field(p => p.message).Query("User2")),
            subQuery => subQuery.MatchPhrase(n => n.Field(p => p.message).Query("User3")),
            subQuery => subQuery.MatchPhrase(n => n.Field(p => p.message).Query("User4"))
         ).MinimumShouldMatch(1)
         .Filter(
                subQuery => subQuery.DateRange(v => v.Field(p => p.timestamp).GreaterThanOrEquals(FromDate)),
                subQuery => subQuery.DateRange(v => v.Field(p => p.timestamp).LessThanOrEquals(EndDate))
            )
 )));

0 个答案:

没有答案