0

Using the C# NEST API on Elasticsearch:

var searchResults = client.Search<Product>(s => s
                .Index(Constants.ElasticSearchIndex)
                .Query(q => q
                    .Raw(jsonRequest)
                )
            );

The query is supposed to run on the /sc_all/ index, but it runs on /sc_all/product/ index (which doesn't exist - /product/ seems to be added because of the Search since T = product).

If I do like this, /product/ is replaced with the value of the constant, i.e. /sc_all/product/ => /sc_all/constant_value/:

var searchResults = client.Search<Product>(s => s
                .Index(Constants.ElasticSearchIndex)
                .Type(Constants.ElasticSearchType)
                .Query(q => q
                    .Raw(jsonRequest)
                )
            );

What should I do if I just want to query /sc_all/ and nothing else?

Thanks!


Json request:

"{\"filtered\": {\"query\": {\"match_all\": { }},\"filter\": {\"nested\" : {\"path\" : \"products\",\"filter\": {\"nested\" : {\"path\" : \"products.da\",\"filter\": { \"bool\": { \"must\": [{\"query\": {\"query_string\": {\"default_field\" : \"products.da.content\", \"query\" : \"kildemoes\"}}}]}}}}}}}}, \"from\": 0, \"size\": 100"

2 Answers 2

1

You just need to specify to run across all types with .AllTypes()

var jsonRequest = "{ \"match_all\": {} }";

var searchResults = client.Search<Product>(s => s
                        .Index(Constants.ElasticSearchIndex)
                        .AllTypes()
                        .Query(q => q
                            .Raw(jsonRequest)
                        )
                    );

which will generate the following request

POST http://localhost:9200/sc_all/_search
{
  "query": { "match_all": {} }
}

Bear in mind that any documents returned will attempt to be deserialized into instances of Product so if you will be targeting multiple different types, you may want to use a common base type or dynamic and additionally, take advantage of covariant search results.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but my Json request already contains a "match_all". I have added it to my question above.
@Louisa I think you've missed the point - if you specify .AllTypes() on the body of the search request, then you will be searching across all types in the index Constants.ElasticSearchIndex. I added an example value for jsonRequest so the code you've given will compile.
0

You were using an outdated version client, like 5.x. I came across the same problem too using 5.x. The second subpath is the document type, which is the _type name of your document and is docs by default. So, the solution I use is to add [ElasticsearchType(Name = "docs")] at the top of my POCO class and the search path will is something like /{indexname}/docs/_search, and it's fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.