6

This may be a silly question, but how do I filter on an empty string in ElasticSearch using Nest. Specifically, how do I recreate the following result:

curl http://localhost:9200/test/event/_search
{
    "filter" : { "term" : { "target" : "" }}
}

I've tried:

(f => f
    .Term("target", "")
);

which according to ElasticSearch and Nest filtering does not work is treated like a conditionless query and returns everything, while adding a .Strict() throws a DslException:

(f => f
    .Strict().Term("target", "")
);

I've also tried .Missing() and .Exists() to no avail.

The relevant section of my _mapping for reference:

{
    "event": {
        "dynamic": "false",
        "properties": {
            target": {
                "type": "string",
                "index": "not_analyzed",
                "store": true,
                "omit_norms": true,
                "index_options": "docs"
            }
        }
    }
}

Any pointers would be greatly appreciated.

4
  • Dont know why the src always throw DslException if Strict = true, but you can send the request "filter" : { "term" : { "target" : "" }} directly using QueryRaw/FilterRaw Commented Dec 4, 2013 at 4:08
  • Thanks, I'm trying to avoid that since it would take a sizeable refactor on my end. I was hoping there was some magic I was missing. Commented Dec 5, 2013 at 3:16
  • I think you can post a bug to NEST to get helped. Maybe the author can suggest some better alternatives. Commented Dec 6, 2013 at 8:41
  • This is not a use case I envisioned but it's a really valid one. I'm working on adding .Verbatim() as a counter part to .Strict() which will not throw or remove the query if its conditionless but leave it as as defined. I will post an answer when its up on github. Commented Dec 6, 2013 at 16:30

2 Answers 2

10

As the documentation on NEST and writing queries mentions you can toggle Strict() mode to trigger exceptions if a part of your query turns out to be conditionless but if thats what you really wanted then you were stuck as you've found out.

I just committed a .Verbatim() construct which works exactly like .Strict() but instead of throwing an exception it will take the query as is and render it as specified.

(f => f
    .Verbatim()
    .Term("target", "")
);

Should thus disable the conditionless query rewrite and insert the query literally as specified.

This will make it in the next version of NEST (so after the current version of 0.12.0.0)

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

Comments

3

I will just remark that you have to use Verbatim() on every query, not just once on the top.

var searchResults = this.Client.Search<Project>(s => s
    .Query(q => q
        //.Verbatim() // no, here won't work
        .Bool(b => b
            .Should(
                bs => bs.Match(p => p.Query("hello").Field("name").Verbatim()),
                bs => bs.Match(p => p.Query("world").Field("name").Verbatim())
            )
        )
    )
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.