Search by multiple values using NEST(ElasticSearch)

故事扮演 提交于 2021-01-29 07:07:52

问题


I have an index called "campaigns" with these records:

"hits" : [
  {
    "_index" : "campaigns",
    "_id" : "cf08b05c-c8b5-45cb-bca8-17267c3613fb",
    "_source" : {
      "PublisherId" : 1,
      "CurrentStatus" : "Pending"
    }
  },
  {
    "_index" : "campaigns",
    "_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
    "_source" : {
      "PublisherId" : 1,
      "CurrentStatus" : "Approved"
    }
  }, 
{
    "_index" : "campaigns",
    "_id" : "21436cb1-583e-4fb4-92e4-4e06ecad23a2",
    "_source" : {
      "PublisherId" : 1,
      "CurrentStatus" : "Rejected"
    }
  }
]

I want to get all campaigns with "PublisherId = 1" and with any statuses between "Approved,Rejected". Something like this:

var statuses = new[] {CampaignStatus.Approved,CampaignStatus.Rejected};
campaigns.Where(c=> c.PublisherId == 1 && statuses.Contains(c.CurrentStatus)).ToList();

How can I run this query using NEST?

Expected Result:

"hits" : [
  {
    "_index" : "campaigns",
    "_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
    "_source" : {
      "PublisherId" : 1,
      "CurrentStatus" : "Approved"
    }
  }, 
{
    "_index" : "campaigns",
    "_id" : "39436cb3-483e-4fb4-92e4-4e06ecad27a1",
    "_source" : {
      "PublisherId" : 1,
      "CurrentStatus" : "Rejected"
    }
  }
]

回答1:


I don't know the syntax of nest but as ES is REST based , providing working example query in JSON format, which you can convert to nest code.

Index mapping

{
    "mappings": {
        "properties": {
            "PublisherId": {
                "type": "integer"
            },
            "CurrentStatus": {
                "type": "text"
            }
        }
    }
}

Index all three sample docs and use below search query

{
    "query": {
        "bool": {
            "must": {
                "term": {
                    "PublisherId": 1
                }
            },
            "should": [
                {
                    "match": {
                        "CurrentStatus": "Rejected"
                    }
                },
                {
                     "match": {
                        "CurrentStatus": "Approved"
                    }
                }
            ],
            "minimum_should_match" : 1
        }
    }
}

Search Result

"hits": [
            {
                "_index": "stof_63968525",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.9808291,
                "_source": {
                    "PublisherId": 1,
                    "CurrentStatus": "Approved"
                }
            },
            {
                "_index": "stof_63968525",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.9808291,
                "_source": {
                    "PublisherId": 1,
                    "CurrentStatus": "Rejected"
                }
            }
        ]

Please note the use of minimum_should_match which forces atleast one of status Rejected and Approved to match and refer bool query in ES to understand the query construct.




回答2:


Did you try this?

            QueryContainer queryAnd = new TermQuery() { Field = "PublisherId", Value = 1 };
            QueryContainer queryOr = new TermQuery() { Field = "CurrentStatus", Value = "Approved" };
                           queryOr |= new TermQuery() { Field = "CurrentStatus", Value = "Rejected" };
            QueryContainer queryMain = queryAnd & queryOr;

            ISearchResponse<campaigns> searchReponse = elasticClient.Search<campaigns>(s => s
                .Query(q2 => q2
                            .Bool(b => b
                            .Should(queryMain)
                        )));


来源:https://stackoverflow.com/questions/63968525/search-by-multiple-values-using-nestelasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!