NEST - MultiMatch search all documents when term is empty - Elasticsearch 6.4











up vote
0
down vote

favorite












I am trying to query using NEST and the multimatch option, but the results are not coming out as expected.



I submit a term that should be compared to several fields. However, if you do not set a search term, all documents must be returned.



I saw that it was possible to use a keyword like "*. *" But it did not work.
Any suggestion?



var searchResponse = client.Search<DocumentElasticModel>(s => s
.Size(pageSize)
.Skip(currentPageIndex * pageSize)
.Sort(ss => ss
.Descending(SortSpecialField.Score)
)
.Source(sf => sf
.Includes(i => i
.Fields(
returnedFields
)
)
)
.Query(q => q
.Nested(c => c
.Name("named_query")
.Boost(1.1)
.InnerHits(i => i.Explain())
.Path(p => p.PerguntasRespostas)
.Query(nq => nq
.MultiMatch(m => m
.Fields(f => filterFields)
-----------------------WHEN THE 'SEARCH' IS EMPTY, SHOULD FIND ALL -----------------
.Query(string.IsNullOrEmpty(search) ? string.Empty : search)
)
)
.IgnoreUnmapped()
)
)









share|improve this question


























    up vote
    0
    down vote

    favorite












    I am trying to query using NEST and the multimatch option, but the results are not coming out as expected.



    I submit a term that should be compared to several fields. However, if you do not set a search term, all documents must be returned.



    I saw that it was possible to use a keyword like "*. *" But it did not work.
    Any suggestion?



    var searchResponse = client.Search<DocumentElasticModel>(s => s
    .Size(pageSize)
    .Skip(currentPageIndex * pageSize)
    .Sort(ss => ss
    .Descending(SortSpecialField.Score)
    )
    .Source(sf => sf
    .Includes(i => i
    .Fields(
    returnedFields
    )
    )
    )
    .Query(q => q
    .Nested(c => c
    .Name("named_query")
    .Boost(1.1)
    .InnerHits(i => i.Explain())
    .Path(p => p.PerguntasRespostas)
    .Query(nq => nq
    .MultiMatch(m => m
    .Fields(f => filterFields)
    -----------------------WHEN THE 'SEARCH' IS EMPTY, SHOULD FIND ALL -----------------
    .Query(string.IsNullOrEmpty(search) ? string.Empty : search)
    )
    )
    .IgnoreUnmapped()
    )
    )









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to query using NEST and the multimatch option, but the results are not coming out as expected.



      I submit a term that should be compared to several fields. However, if you do not set a search term, all documents must be returned.



      I saw that it was possible to use a keyword like "*. *" But it did not work.
      Any suggestion?



      var searchResponse = client.Search<DocumentElasticModel>(s => s
      .Size(pageSize)
      .Skip(currentPageIndex * pageSize)
      .Sort(ss => ss
      .Descending(SortSpecialField.Score)
      )
      .Source(sf => sf
      .Includes(i => i
      .Fields(
      returnedFields
      )
      )
      )
      .Query(q => q
      .Nested(c => c
      .Name("named_query")
      .Boost(1.1)
      .InnerHits(i => i.Explain())
      .Path(p => p.PerguntasRespostas)
      .Query(nq => nq
      .MultiMatch(m => m
      .Fields(f => filterFields)
      -----------------------WHEN THE 'SEARCH' IS EMPTY, SHOULD FIND ALL -----------------
      .Query(string.IsNullOrEmpty(search) ? string.Empty : search)
      )
      )
      .IgnoreUnmapped()
      )
      )









      share|improve this question













      I am trying to query using NEST and the multimatch option, but the results are not coming out as expected.



      I submit a term that should be compared to several fields. However, if you do not set a search term, all documents must be returned.



      I saw that it was possible to use a keyword like "*. *" But it did not work.
      Any suggestion?



      var searchResponse = client.Search<DocumentElasticModel>(s => s
      .Size(pageSize)
      .Skip(currentPageIndex * pageSize)
      .Sort(ss => ss
      .Descending(SortSpecialField.Score)
      )
      .Source(sf => sf
      .Includes(i => i
      .Fields(
      returnedFields
      )
      )
      )
      .Query(q => q
      .Nested(c => c
      .Name("named_query")
      .Boost(1.1)
      .InnerHits(i => i.Explain())
      .Path(p => p.PerguntasRespostas)
      .Query(nq => nq
      .MultiMatch(m => m
      .Fields(f => filterFields)
      -----------------------WHEN THE 'SEARCH' IS EMPTY, SHOULD FIND ALL -----------------
      .Query(string.IsNullOrEmpty(search) ? string.Empty : search)
      )
      )
      .IgnoreUnmapped()
      )
      )






      c# elasticsearch nest






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 10:49









      Cesar

      1113




      1113
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          NEST supports this by default with a concept referred to as Conditionless queries. You can



          private static void Main()
          {
          var defaultIndex = "documents";
          var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

          var settings = new ConnectionSettings(pool, new InMemoryConnection())
          .DefaultIndex(defaultIndex)
          .DisableDirectStreaming()
          .PrettyJson()
          .OnRequestCompleted(callDetails =>
          {
          if (callDetails.RequestBodyInBytes != null)
          {
          Console.WriteLine(
          $"{callDetails.HttpMethod} {callDetails.Uri} n" +
          $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
          }
          else
          {
          Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
          }

          Console.WriteLine();

          if (callDetails.ResponseBodyInBytes != null)
          {
          Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
          $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}n" +
          $"{new string('-', 30)}n");
          }
          else
          {
          Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
          $"{new string('-', 30)}n");
          }
          });

          var client = new ElasticClient(settings);

          var pageSize = 20;
          var currentPageIndex = 0;
          string search = "foo";


          var searchResponse = client.Search<DocumentElasticModel>(s => s
          .Size(pageSize)
          .Skip(currentPageIndex * pageSize)
          .Sort(ss => ss
          .Descending(SortSpecialField.Score)
          )
          .Source(sf => sf
          .Includes(i => i
          .Fields(f => f.TopLevelMessage)
          )
          )
          .Query(q => q
          .Nested(c => c
          .Name("named_query")
          .Boost(1.1)
          .InnerHits(i => i.Explain())
          .Path(p => p.PerguntasRespostas)
          .Query(nq => nq
          .MultiMatch(m => m
          .Fields(f => f
          .Field(ff => ff.PerguntasRespostas.First().Message)
          )
          .Query(search)
          )
          )
          .IgnoreUnmapped()
          )
          )
          );
          }


          public class DocumentElasticModel
          {
          public string TopLevelMessage { get; set; }

          public IEnumerable<PerguntasRespostas> PerguntasRespostas {get;set;}
          }

          public class PerguntasRespostas
          {
          public string Message { get; set; }
          }


          This would send the following query



          POST http://localhost:9200/documents/documentelasticmodel/_search
          {
          "from": 0,
          "size": 20,
          "sort": [
          {
          "_score": {
          "order": "desc"
          }
          }
          ],
          "_source": {
          "includes": [
          "topLevelMessage"
          ]
          },
          "query": {
          "nested": {
          "_name": "named_query",
          "boost": 1.1,
          "query": {
          "multi_match": {
          "query": "foo",
          "fields": [
          "perguntasRespostas.message"
          ]
          }
          },
          "path": "perguntasRespostas",
          "inner_hits": {
          "explain": true
          },
          "ignore_unmapped": true
          }
          }
          }


          Now, if you change search to string.Empty or null, you get



          POST http://localhost:9200/documents/documentelasticmodel/_search
          {
          "from": 0,
          "size": 20,
          "sort": [
          {
          "_score": {
          "order": "desc"
          }
          }
          ],
          "_source": {
          "includes": [
          "topLevelMessage"
          ]
          }
          }


          With no explicit "query" in the request, this is the same as a match_all query.



          If you wanted to override the conditionless query feature in NEST, you can mark the query as .Verbatim() and NEST will send exactly as is



          var searchResponse = client.Search<DocumentElasticModel>(s => s
          .Size(pageSize)
          .Skip(currentPageIndex * pageSize)
          .Sort(ss => ss
          .Descending(SortSpecialField.Score)
          )
          .Source(sf => sf
          .Includes(i => i
          .Fields(f => f.TopLevelMessage)
          )
          )
          .Query(q => q
          .Nested(c => c
          .Verbatim() // <-- mark the nested query
          .Name("named_query")
          .Boost(1.1)
          .InnerHits(i => i.Explain())
          .Path(p => p.PerguntasRespostas)
          .Query(nq => nq
          .MultiMatch(m => m
          .Verbatim() // <-- mark the inner query
          .Fields(f => f
          .Field(ff => ff.PerguntasRespostas.First().Message)
          )
          .Query(search)
          )
          )
          .IgnoreUnmapped()
          )
          )
          );


          which sends



          POST http://localhost:9200/documents/documentelasticmodel/_search
          {
          "from": 0,
          "size": 20,
          "sort": [
          {
          "_score": {
          "order": "desc"
          }
          }
          ],
          "_source": {
          "includes": [
          "topLevelMessage"
          ]
          },
          "query": {
          "nested": {
          "_name": "named_query",
          "boost": 1.1,
          "query": {
          "multi_match": {
          "fields": [
          "perguntasRespostas.message"
          ]
          }
          },
          "path": "perguntasRespostas",
          "inner_hits": {
          "explain": true
          },
          "ignore_unmapped": true
          }
          }
          }


          You'll need to check if this is a valid query that Elasticsearch accepts.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224264%2fnest-multimatch-search-all-documents-when-term-is-empty-elasticsearch-6-4%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            NEST supports this by default with a concept referred to as Conditionless queries. You can



            private static void Main()
            {
            var defaultIndex = "documents";
            var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

            var settings = new ConnectionSettings(pool, new InMemoryConnection())
            .DefaultIndex(defaultIndex)
            .DisableDirectStreaming()
            .PrettyJson()
            .OnRequestCompleted(callDetails =>
            {
            if (callDetails.RequestBodyInBytes != null)
            {
            Console.WriteLine(
            $"{callDetails.HttpMethod} {callDetails.Uri} n" +
            $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
            }
            else
            {
            Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
            }

            Console.WriteLine();

            if (callDetails.ResponseBodyInBytes != null)
            {
            Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
            $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}n" +
            $"{new string('-', 30)}n");
            }
            else
            {
            Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
            $"{new string('-', 30)}n");
            }
            });

            var client = new ElasticClient(settings);

            var pageSize = 20;
            var currentPageIndex = 0;
            string search = "foo";


            var searchResponse = client.Search<DocumentElasticModel>(s => s
            .Size(pageSize)
            .Skip(currentPageIndex * pageSize)
            .Sort(ss => ss
            .Descending(SortSpecialField.Score)
            )
            .Source(sf => sf
            .Includes(i => i
            .Fields(f => f.TopLevelMessage)
            )
            )
            .Query(q => q
            .Nested(c => c
            .Name("named_query")
            .Boost(1.1)
            .InnerHits(i => i.Explain())
            .Path(p => p.PerguntasRespostas)
            .Query(nq => nq
            .MultiMatch(m => m
            .Fields(f => f
            .Field(ff => ff.PerguntasRespostas.First().Message)
            )
            .Query(search)
            )
            )
            .IgnoreUnmapped()
            )
            )
            );
            }


            public class DocumentElasticModel
            {
            public string TopLevelMessage { get; set; }

            public IEnumerable<PerguntasRespostas> PerguntasRespostas {get;set;}
            }

            public class PerguntasRespostas
            {
            public string Message { get; set; }
            }


            This would send the following query



            POST http://localhost:9200/documents/documentelasticmodel/_search
            {
            "from": 0,
            "size": 20,
            "sort": [
            {
            "_score": {
            "order": "desc"
            }
            }
            ],
            "_source": {
            "includes": [
            "topLevelMessage"
            ]
            },
            "query": {
            "nested": {
            "_name": "named_query",
            "boost": 1.1,
            "query": {
            "multi_match": {
            "query": "foo",
            "fields": [
            "perguntasRespostas.message"
            ]
            }
            },
            "path": "perguntasRespostas",
            "inner_hits": {
            "explain": true
            },
            "ignore_unmapped": true
            }
            }
            }


            Now, if you change search to string.Empty or null, you get



            POST http://localhost:9200/documents/documentelasticmodel/_search
            {
            "from": 0,
            "size": 20,
            "sort": [
            {
            "_score": {
            "order": "desc"
            }
            }
            ],
            "_source": {
            "includes": [
            "topLevelMessage"
            ]
            }
            }


            With no explicit "query" in the request, this is the same as a match_all query.



            If you wanted to override the conditionless query feature in NEST, you can mark the query as .Verbatim() and NEST will send exactly as is



            var searchResponse = client.Search<DocumentElasticModel>(s => s
            .Size(pageSize)
            .Skip(currentPageIndex * pageSize)
            .Sort(ss => ss
            .Descending(SortSpecialField.Score)
            )
            .Source(sf => sf
            .Includes(i => i
            .Fields(f => f.TopLevelMessage)
            )
            )
            .Query(q => q
            .Nested(c => c
            .Verbatim() // <-- mark the nested query
            .Name("named_query")
            .Boost(1.1)
            .InnerHits(i => i.Explain())
            .Path(p => p.PerguntasRespostas)
            .Query(nq => nq
            .MultiMatch(m => m
            .Verbatim() // <-- mark the inner query
            .Fields(f => f
            .Field(ff => ff.PerguntasRespostas.First().Message)
            )
            .Query(search)
            )
            )
            .IgnoreUnmapped()
            )
            )
            );


            which sends



            POST http://localhost:9200/documents/documentelasticmodel/_search
            {
            "from": 0,
            "size": 20,
            "sort": [
            {
            "_score": {
            "order": "desc"
            }
            }
            ],
            "_source": {
            "includes": [
            "topLevelMessage"
            ]
            },
            "query": {
            "nested": {
            "_name": "named_query",
            "boost": 1.1,
            "query": {
            "multi_match": {
            "fields": [
            "perguntasRespostas.message"
            ]
            }
            },
            "path": "perguntasRespostas",
            "inner_hits": {
            "explain": true
            },
            "ignore_unmapped": true
            }
            }
            }


            You'll need to check if this is a valid query that Elasticsearch accepts.






            share|improve this answer

























              up vote
              0
              down vote













              NEST supports this by default with a concept referred to as Conditionless queries. You can



              private static void Main()
              {
              var defaultIndex = "documents";
              var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

              var settings = new ConnectionSettings(pool, new InMemoryConnection())
              .DefaultIndex(defaultIndex)
              .DisableDirectStreaming()
              .PrettyJson()
              .OnRequestCompleted(callDetails =>
              {
              if (callDetails.RequestBodyInBytes != null)
              {
              Console.WriteLine(
              $"{callDetails.HttpMethod} {callDetails.Uri} n" +
              $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
              }
              else
              {
              Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
              }

              Console.WriteLine();

              if (callDetails.ResponseBodyInBytes != null)
              {
              Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
              $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}n" +
              $"{new string('-', 30)}n");
              }
              else
              {
              Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
              $"{new string('-', 30)}n");
              }
              });

              var client = new ElasticClient(settings);

              var pageSize = 20;
              var currentPageIndex = 0;
              string search = "foo";


              var searchResponse = client.Search<DocumentElasticModel>(s => s
              .Size(pageSize)
              .Skip(currentPageIndex * pageSize)
              .Sort(ss => ss
              .Descending(SortSpecialField.Score)
              )
              .Source(sf => sf
              .Includes(i => i
              .Fields(f => f.TopLevelMessage)
              )
              )
              .Query(q => q
              .Nested(c => c
              .Name("named_query")
              .Boost(1.1)
              .InnerHits(i => i.Explain())
              .Path(p => p.PerguntasRespostas)
              .Query(nq => nq
              .MultiMatch(m => m
              .Fields(f => f
              .Field(ff => ff.PerguntasRespostas.First().Message)
              )
              .Query(search)
              )
              )
              .IgnoreUnmapped()
              )
              )
              );
              }


              public class DocumentElasticModel
              {
              public string TopLevelMessage { get; set; }

              public IEnumerable<PerguntasRespostas> PerguntasRespostas {get;set;}
              }

              public class PerguntasRespostas
              {
              public string Message { get; set; }
              }


              This would send the following query



              POST http://localhost:9200/documents/documentelasticmodel/_search
              {
              "from": 0,
              "size": 20,
              "sort": [
              {
              "_score": {
              "order": "desc"
              }
              }
              ],
              "_source": {
              "includes": [
              "topLevelMessage"
              ]
              },
              "query": {
              "nested": {
              "_name": "named_query",
              "boost": 1.1,
              "query": {
              "multi_match": {
              "query": "foo",
              "fields": [
              "perguntasRespostas.message"
              ]
              }
              },
              "path": "perguntasRespostas",
              "inner_hits": {
              "explain": true
              },
              "ignore_unmapped": true
              }
              }
              }


              Now, if you change search to string.Empty or null, you get



              POST http://localhost:9200/documents/documentelasticmodel/_search
              {
              "from": 0,
              "size": 20,
              "sort": [
              {
              "_score": {
              "order": "desc"
              }
              }
              ],
              "_source": {
              "includes": [
              "topLevelMessage"
              ]
              }
              }


              With no explicit "query" in the request, this is the same as a match_all query.



              If you wanted to override the conditionless query feature in NEST, you can mark the query as .Verbatim() and NEST will send exactly as is



              var searchResponse = client.Search<DocumentElasticModel>(s => s
              .Size(pageSize)
              .Skip(currentPageIndex * pageSize)
              .Sort(ss => ss
              .Descending(SortSpecialField.Score)
              )
              .Source(sf => sf
              .Includes(i => i
              .Fields(f => f.TopLevelMessage)
              )
              )
              .Query(q => q
              .Nested(c => c
              .Verbatim() // <-- mark the nested query
              .Name("named_query")
              .Boost(1.1)
              .InnerHits(i => i.Explain())
              .Path(p => p.PerguntasRespostas)
              .Query(nq => nq
              .MultiMatch(m => m
              .Verbatim() // <-- mark the inner query
              .Fields(f => f
              .Field(ff => ff.PerguntasRespostas.First().Message)
              )
              .Query(search)
              )
              )
              .IgnoreUnmapped()
              )
              )
              );


              which sends



              POST http://localhost:9200/documents/documentelasticmodel/_search
              {
              "from": 0,
              "size": 20,
              "sort": [
              {
              "_score": {
              "order": "desc"
              }
              }
              ],
              "_source": {
              "includes": [
              "topLevelMessage"
              ]
              },
              "query": {
              "nested": {
              "_name": "named_query",
              "boost": 1.1,
              "query": {
              "multi_match": {
              "fields": [
              "perguntasRespostas.message"
              ]
              }
              },
              "path": "perguntasRespostas",
              "inner_hits": {
              "explain": true
              },
              "ignore_unmapped": true
              }
              }
              }


              You'll need to check if this is a valid query that Elasticsearch accepts.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                NEST supports this by default with a concept referred to as Conditionless queries. You can



                private static void Main()
                {
                var defaultIndex = "documents";
                var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

                var settings = new ConnectionSettings(pool, new InMemoryConnection())
                .DefaultIndex(defaultIndex)
                .DisableDirectStreaming()
                .PrettyJson()
                .OnRequestCompleted(callDetails =>
                {
                if (callDetails.RequestBodyInBytes != null)
                {
                Console.WriteLine(
                $"{callDetails.HttpMethod} {callDetails.Uri} n" +
                $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
                }
                else
                {
                Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
                }

                Console.WriteLine();

                if (callDetails.ResponseBodyInBytes != null)
                {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
                $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}n" +
                $"{new string('-', 30)}n");
                }
                else
                {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
                $"{new string('-', 30)}n");
                }
                });

                var client = new ElasticClient(settings);

                var pageSize = 20;
                var currentPageIndex = 0;
                string search = "foo";


                var searchResponse = client.Search<DocumentElasticModel>(s => s
                .Size(pageSize)
                .Skip(currentPageIndex * pageSize)
                .Sort(ss => ss
                .Descending(SortSpecialField.Score)
                )
                .Source(sf => sf
                .Includes(i => i
                .Fields(f => f.TopLevelMessage)
                )
                )
                .Query(q => q
                .Nested(c => c
                .Name("named_query")
                .Boost(1.1)
                .InnerHits(i => i.Explain())
                .Path(p => p.PerguntasRespostas)
                .Query(nq => nq
                .MultiMatch(m => m
                .Fields(f => f
                .Field(ff => ff.PerguntasRespostas.First().Message)
                )
                .Query(search)
                )
                )
                .IgnoreUnmapped()
                )
                )
                );
                }


                public class DocumentElasticModel
                {
                public string TopLevelMessage { get; set; }

                public IEnumerable<PerguntasRespostas> PerguntasRespostas {get;set;}
                }

                public class PerguntasRespostas
                {
                public string Message { get; set; }
                }


                This would send the following query



                POST http://localhost:9200/documents/documentelasticmodel/_search
                {
                "from": 0,
                "size": 20,
                "sort": [
                {
                "_score": {
                "order": "desc"
                }
                }
                ],
                "_source": {
                "includes": [
                "topLevelMessage"
                ]
                },
                "query": {
                "nested": {
                "_name": "named_query",
                "boost": 1.1,
                "query": {
                "multi_match": {
                "query": "foo",
                "fields": [
                "perguntasRespostas.message"
                ]
                }
                },
                "path": "perguntasRespostas",
                "inner_hits": {
                "explain": true
                },
                "ignore_unmapped": true
                }
                }
                }


                Now, if you change search to string.Empty or null, you get



                POST http://localhost:9200/documents/documentelasticmodel/_search
                {
                "from": 0,
                "size": 20,
                "sort": [
                {
                "_score": {
                "order": "desc"
                }
                }
                ],
                "_source": {
                "includes": [
                "topLevelMessage"
                ]
                }
                }


                With no explicit "query" in the request, this is the same as a match_all query.



                If you wanted to override the conditionless query feature in NEST, you can mark the query as .Verbatim() and NEST will send exactly as is



                var searchResponse = client.Search<DocumentElasticModel>(s => s
                .Size(pageSize)
                .Skip(currentPageIndex * pageSize)
                .Sort(ss => ss
                .Descending(SortSpecialField.Score)
                )
                .Source(sf => sf
                .Includes(i => i
                .Fields(f => f.TopLevelMessage)
                )
                )
                .Query(q => q
                .Nested(c => c
                .Verbatim() // <-- mark the nested query
                .Name("named_query")
                .Boost(1.1)
                .InnerHits(i => i.Explain())
                .Path(p => p.PerguntasRespostas)
                .Query(nq => nq
                .MultiMatch(m => m
                .Verbatim() // <-- mark the inner query
                .Fields(f => f
                .Field(ff => ff.PerguntasRespostas.First().Message)
                )
                .Query(search)
                )
                )
                .IgnoreUnmapped()
                )
                )
                );


                which sends



                POST http://localhost:9200/documents/documentelasticmodel/_search
                {
                "from": 0,
                "size": 20,
                "sort": [
                {
                "_score": {
                "order": "desc"
                }
                }
                ],
                "_source": {
                "includes": [
                "topLevelMessage"
                ]
                },
                "query": {
                "nested": {
                "_name": "named_query",
                "boost": 1.1,
                "query": {
                "multi_match": {
                "fields": [
                "perguntasRespostas.message"
                ]
                }
                },
                "path": "perguntasRespostas",
                "inner_hits": {
                "explain": true
                },
                "ignore_unmapped": true
                }
                }
                }


                You'll need to check if this is a valid query that Elasticsearch accepts.






                share|improve this answer












                NEST supports this by default with a concept referred to as Conditionless queries. You can



                private static void Main()
                {
                var defaultIndex = "documents";
                var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

                var settings = new ConnectionSettings(pool, new InMemoryConnection())
                .DefaultIndex(defaultIndex)
                .DisableDirectStreaming()
                .PrettyJson()
                .OnRequestCompleted(callDetails =>
                {
                if (callDetails.RequestBodyInBytes != null)
                {
                Console.WriteLine(
                $"{callDetails.HttpMethod} {callDetails.Uri} n" +
                $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
                }
                else
                {
                Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
                }

                Console.WriteLine();

                if (callDetails.ResponseBodyInBytes != null)
                {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
                $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}n" +
                $"{new string('-', 30)}n");
                }
                else
                {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}n" +
                $"{new string('-', 30)}n");
                }
                });

                var client = new ElasticClient(settings);

                var pageSize = 20;
                var currentPageIndex = 0;
                string search = "foo";


                var searchResponse = client.Search<DocumentElasticModel>(s => s
                .Size(pageSize)
                .Skip(currentPageIndex * pageSize)
                .Sort(ss => ss
                .Descending(SortSpecialField.Score)
                )
                .Source(sf => sf
                .Includes(i => i
                .Fields(f => f.TopLevelMessage)
                )
                )
                .Query(q => q
                .Nested(c => c
                .Name("named_query")
                .Boost(1.1)
                .InnerHits(i => i.Explain())
                .Path(p => p.PerguntasRespostas)
                .Query(nq => nq
                .MultiMatch(m => m
                .Fields(f => f
                .Field(ff => ff.PerguntasRespostas.First().Message)
                )
                .Query(search)
                )
                )
                .IgnoreUnmapped()
                )
                )
                );
                }


                public class DocumentElasticModel
                {
                public string TopLevelMessage { get; set; }

                public IEnumerable<PerguntasRespostas> PerguntasRespostas {get;set;}
                }

                public class PerguntasRespostas
                {
                public string Message { get; set; }
                }


                This would send the following query



                POST http://localhost:9200/documents/documentelasticmodel/_search
                {
                "from": 0,
                "size": 20,
                "sort": [
                {
                "_score": {
                "order": "desc"
                }
                }
                ],
                "_source": {
                "includes": [
                "topLevelMessage"
                ]
                },
                "query": {
                "nested": {
                "_name": "named_query",
                "boost": 1.1,
                "query": {
                "multi_match": {
                "query": "foo",
                "fields": [
                "perguntasRespostas.message"
                ]
                }
                },
                "path": "perguntasRespostas",
                "inner_hits": {
                "explain": true
                },
                "ignore_unmapped": true
                }
                }
                }


                Now, if you change search to string.Empty or null, you get



                POST http://localhost:9200/documents/documentelasticmodel/_search
                {
                "from": 0,
                "size": 20,
                "sort": [
                {
                "_score": {
                "order": "desc"
                }
                }
                ],
                "_source": {
                "includes": [
                "topLevelMessage"
                ]
                }
                }


                With no explicit "query" in the request, this is the same as a match_all query.



                If you wanted to override the conditionless query feature in NEST, you can mark the query as .Verbatim() and NEST will send exactly as is



                var searchResponse = client.Search<DocumentElasticModel>(s => s
                .Size(pageSize)
                .Skip(currentPageIndex * pageSize)
                .Sort(ss => ss
                .Descending(SortSpecialField.Score)
                )
                .Source(sf => sf
                .Includes(i => i
                .Fields(f => f.TopLevelMessage)
                )
                )
                .Query(q => q
                .Nested(c => c
                .Verbatim() // <-- mark the nested query
                .Name("named_query")
                .Boost(1.1)
                .InnerHits(i => i.Explain())
                .Path(p => p.PerguntasRespostas)
                .Query(nq => nq
                .MultiMatch(m => m
                .Verbatim() // <-- mark the inner query
                .Fields(f => f
                .Field(ff => ff.PerguntasRespostas.First().Message)
                )
                .Query(search)
                )
                )
                .IgnoreUnmapped()
                )
                )
                );


                which sends



                POST http://localhost:9200/documents/documentelasticmodel/_search
                {
                "from": 0,
                "size": 20,
                "sort": [
                {
                "_score": {
                "order": "desc"
                }
                }
                ],
                "_source": {
                "includes": [
                "topLevelMessage"
                ]
                },
                "query": {
                "nested": {
                "_name": "named_query",
                "boost": 1.1,
                "query": {
                "multi_match": {
                "fields": [
                "perguntasRespostas.message"
                ]
                }
                },
                "path": "perguntasRespostas",
                "inner_hits": {
                "explain": true
                },
                "ignore_unmapped": true
                }
                }
                }


                You'll need to check if this is a valid query that Elasticsearch accepts.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 0:02









                Russ Cam

                102k23167225




                102k23167225






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224264%2fnest-multimatch-search-all-documents-when-term-is-empty-elasticsearch-6-4%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Hercules Kyvelos

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud