Search code examples
c#elasticsearchnestelasticsearch.net

Why do the NEST ElasticClient did not find a document?


When I use Kibana to execute the following Searchrequest to Elasticsearch

GET _search
{
  "query": {
    "query_string": {
      "query": "PDB_W2237.docx", 
      "default_operator": "AND"
    }
  }
}

it returns:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 15,
    "successful": 15,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 6.3527603,
    "hits": [
      {
        "_index": "proconact",
        "_type": "proconact",
        "_id": "68cecf2c-7e5a-11e5-80fa-000c29bd9450",
        "_score": 6.3527603,
        "_source": {
          "Id": "68cecf2c-7e5a-11e5-80fa-000c29bd9450",
          "ActivityId": "1bad9115-7e5a-11e5-80fa-000c29bd9450",
          "ProjectId": "08938a1d-2429-11e5-80f9-000c29bd9450",
          "Filename": "PDB_W2237.docx"
        }
      }
    ]
  }
}

When I use the NEST ElasticClient like

var client = new ElasticClient();
var searchResponse = client.Search<Hit>(new SearchRequest {          
    Query = new QueryStringQuery {
         Query = "DB_W2237.docx",
         DefaultOperator = Operator.And
    }
});

it do return 0 Hits.

Here is the Indexmapping for the 4 Fields in the Hit:

{
  "proconact": {
    "mappings": {
      "proconact": {
        "properties": {
          "ActivityId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "Filename": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "Id": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "ProjectId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

Are the two search-requests not the same?


Solution

  • The problem is your mapping doesn't allow a token different than whatever is present in your index.

    In your kibana query:

    GET _search
    {
      "query": {
        "query_string": {
          "query": "PDB_W2237.docx", 
          "default_operator": "AND"
        }
      }
    }
    

    You're querying PDB_W2237.docx but in your NEST you're querying DB_W2237.docx.

    If you want to query DB_W2237.docx and expecting results then you might have to change the analyzer from standard analyzer which is applied by default to something else a possible candidate depends on your usecase.