Home > Back-end >  ElasticSearch NEST DSL Query Cross Fields Query
ElasticSearch NEST DSL Query Cross Fields Query

Time:09-23

I am trying to convert following ElasticSearch DSL Query to NEST and it seems something is not correct. Here is my DSL Query:

{  
  "query": {
    "multi_match": {
            "query": "AJ",
            "type": "cross_fields",            
            "fields": ["name", "shortname", "shortname2", "number"],
            "operator": "and"            
          }
  }
}

I have a POCO class. I want to get result as a List as seen below:

public class SearchDto
    {
        public Guid Id { get; set; }
        public string Number { get; set; }
        public string Name { get; set; }
        public string ShortName2 { get; set; }
        public string ShortName1 { get; set; }
    }

Since it is a Cross Fields query, I have created fields like this:

Fields nameField = Infer.Field<SearchDto>(p => p.Name);
var shortName2 = Infer.Field<SearchDto>(p => p.ShortName2);
var shortName1 = Infer.Field<SearchDto>(p => p.ShortName1);
var number = Infer.Field<SearchDto>(p => p.Number);

Here is my NEST query:

var searchRequest = new SearchRequest() {
   Query = new MultiMatchQuery() {
     Fields = nameField
       .And(shortName2)
       .And(shortName1)
       .And(number),
       Query = value,
       Operator = Operator.And,
       Type = TextQueryType.CrossFields
   }
 }

When I get the Json string for my searchRequest, it only prints "{}" using the following:

var json = _client.RequestResponseSerializer.SerializeToString(searchRequest);

It also posts "{}" as request body

I also tried the following:

var response = _client.Search <List<SearchDto>> (s => s
  .Size(500)
  .Index("mysearchIndex")
  .Query(q => q
    .MultiMatch(m => m
      .Type(TextQueryType.CrossFields)
      .Fields(nameField)
      .Fields(shortName1)
      .Fields(shortName2)
      .Fields(number)
      .Operator(Operator.And)
      .Query(value)
    )
  ));

Above query posts only "{"size" : 500}" to my elasticsearch endpoint

Can someone please suggest what I am doing wrong and/or suggest better way to handle my query using NEST? It is not even building a full query for some reason.

CodePudding user response:

Nest queries are condition less. If input is determined to be null or empty string then the query will be omitted from the request.

In your query is "value" is empty or null , then query generated will be "{}".

If your intention is to search on empty value. Then you need to mark individual query as verbatim

An individual query can be marked as verbatim in order take effect; a verbatim query will be serialized and sent in the request to Elasticsearch, bypassing NEST’s conditionless checks.

Example

var searchRequest = new SearchRequest()
                {
                    Query = new MultiMatchQuery()
                    {
                        Fields = nameField
                       .And(shortName2)
                       .And(shortName1)
                       .And(number),
                        Query = "",
                        Operator = Operator.And,
                        Type = TextQueryType.CrossFields,
                        IsVerbatim=true   ---> note flag
                    }
                };

Corresponding query

{"query":{"multi_match":{"fields":["name","shortName2","shortName1","number"],"operator":"and","query":"","type":"cross_fields"}}}
  • Related