NEST elasticsearch.NET search query not returning

2019-09-02 08:24发布

问题:

I'm using the object initializer syntax with NEST to form a search query. When I include the second pdfQuery with the logical OR operator, I get no results. If I exclude it, I get results.

QueryContainer titleQuery = new MatchQuery
{
    Field = Property.Path<ElasticBook>(p => p.Title),
    Query = query,
    Boost = 50,
    Slop = 2,
    MinimumShouldMatch = "55%"
};

QueryContainer pdfQuery = new MatchQuery
{
    Field = Property.Path<ElasticBook>(p => p.Pdf),
    Query = query,
    CutoffFrequency = 0.001
};

var result = _client.Search<ElasticBook>(new SearchRequest("bookswithstop", "en")
{
    From = 0,
    Size = 10,
    Query = titleQuery || pdfQuery,
    Timeout = "20000",
    Fields = new []
    {
      Property.Path<ElasticBook>(p => p.Title)  
    } 
});

If I debug and inspect the result var, I copy-value one of request properties to get:

{
 "timeout": "20000",
 "from": 0,
 "size": 10,
 "fields": [
   "title"
 ],
 "query": {
   "bool": {
     "should": [
       {
         "match": {
           "title": {
             "query": "Proper Guide To Excel 2010",
             "slop": 2,
             "boost": 50.0,
             "minimum_should_match": "55%"
           }
         }
       },
       {
         "match": {
           "pdf": {
             "query": "Proper Guide To Excel 2010",
             "cutoff_frequency": 0.001
           }
         }
       }
     ]
   }
 }
}

The problem is that if I copy that query into sense - it returns about 100 results (albeit slowly). I've checked the header info and that seems to be correct from NEST as well:

ConnectionStatus = {StatusCode: 200, 
    Method: POST, 
    Url: http://elasticsearch-blablablamrfreeman/bookswithstop/en/_search, 
    Request: {
  "timeout": "20000",
  "from": 0,
  "size": 10,
  "fields": [
    "title"
  ],
  "query": {
    "bool": {
      "shoul...

The pdf field uses the elastic search attachment plugin (located @ https://github.com/elastic/elasticsearch-mapper-attachments) and I was getting Newtonsoft.JSON system.outofmemoryexceptions being thrown before (but not now for some reason).

My only suggestion therefore is that perhaps there's some serialization issue via my query and NEST? If that were the case I'm not sure why it would just execute successfully with a 200 code and give 0 documents in the Documents property

Could anyone please explain to me how I would go about troubleshooting this please? It clearly doesn't like my second search query (pdfQuery) but I'm not sure why - and the resultant JSON request syntax seems to be correct as well!

回答1:

I think this part is causing problems

Fields = new []
    {
      Property.Path<ElasticBook>(p => p.Title)  
    } 

When do you use Fields option, elasticsearch is not returning _source field, so you can't access results through result.Documents. Instead, you have to use result.FieldSelections, which is quite unpleasant.

If you want to return only specific fields from elasticsearch and still be able to use result.Documents you can take advantage of source includes / excludes. With NEST you can do this as follows:

var searchResponse = client.Search<Document>(s => s
    .Source(source => source.Include(f => f.Number))
    .Query(q => q.MatchAll()));

Hope this helps you.