Elastic search query with Nest, search for text, but also filtering by flags





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have the following query:



return Q
.MultiMatch(Fu => Fu
.Fields(F => F
.Field(Ff => Ff.Tags)
.Field(Ff => Ff.Title)
)
.Query(Terms)
.Fuzziness(Fuzziness.EditDistance(2))
);


How can I add a secondary part to the query that would perform:



"If the document has NO 'score' field OR if 'score' <= 1"



I don't see how to add this part with the API.










share|improve this question























  • What do you want to apply the predicate on the score field to? In addition to the multi_match query?

    – Russ Cam
    Nov 17 '18 at 0:39











  • yes; essentially I have a field that I need to use to remove a whole group from the search. maybe the name 'score' in my example could be confusing; let's call it 'flag': I would like to be able to apply the multi_match only to documents that don't have that field, or, if they have it, where it is < 1. All other documents are to be ignored.

    – Thomas
    Nov 17 '18 at 0:42


















0















I have the following query:



return Q
.MultiMatch(Fu => Fu
.Fields(F => F
.Field(Ff => Ff.Tags)
.Field(Ff => Ff.Title)
)
.Query(Terms)
.Fuzziness(Fuzziness.EditDistance(2))
);


How can I add a secondary part to the query that would perform:



"If the document has NO 'score' field OR if 'score' <= 1"



I don't see how to add this part with the API.










share|improve this question























  • What do you want to apply the predicate on the score field to? In addition to the multi_match query?

    – Russ Cam
    Nov 17 '18 at 0:39











  • yes; essentially I have a field that I need to use to remove a whole group from the search. maybe the name 'score' in my example could be confusing; let's call it 'flag': I would like to be able to apply the multi_match only to documents that don't have that field, or, if they have it, where it is < 1. All other documents are to be ignored.

    – Thomas
    Nov 17 '18 at 0:42














0












0








0








I have the following query:



return Q
.MultiMatch(Fu => Fu
.Fields(F => F
.Field(Ff => Ff.Tags)
.Field(Ff => Ff.Title)
)
.Query(Terms)
.Fuzziness(Fuzziness.EditDistance(2))
);


How can I add a secondary part to the query that would perform:



"If the document has NO 'score' field OR if 'score' <= 1"



I don't see how to add this part with the API.










share|improve this question














I have the following query:



return Q
.MultiMatch(Fu => Fu
.Fields(F => F
.Field(Ff => Ff.Tags)
.Field(Ff => Ff.Title)
)
.Query(Terms)
.Fuzziness(Fuzziness.EditDistance(2))
);


How can I add a secondary part to the query that would perform:



"If the document has NO 'score' field OR if 'score' <= 1"



I don't see how to add this part with the API.







elasticsearch nest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 12:47









ThomasThomas

1,27711638




1,27711638













  • What do you want to apply the predicate on the score field to? In addition to the multi_match query?

    – Russ Cam
    Nov 17 '18 at 0:39











  • yes; essentially I have a field that I need to use to remove a whole group from the search. maybe the name 'score' in my example could be confusing; let's call it 'flag': I would like to be able to apply the multi_match only to documents that don't have that field, or, if they have it, where it is < 1. All other documents are to be ignored.

    – Thomas
    Nov 17 '18 at 0:42



















  • What do you want to apply the predicate on the score field to? In addition to the multi_match query?

    – Russ Cam
    Nov 17 '18 at 0:39











  • yes; essentially I have a field that I need to use to remove a whole group from the search. maybe the name 'score' in my example could be confusing; let's call it 'flag': I would like to be able to apply the multi_match only to documents that don't have that field, or, if they have it, where it is < 1. All other documents are to be ignored.

    – Thomas
    Nov 17 '18 at 0:42

















What do you want to apply the predicate on the score field to? In addition to the multi_match query?

– Russ Cam
Nov 17 '18 at 0:39





What do you want to apply the predicate on the score field to? In addition to the multi_match query?

– Russ Cam
Nov 17 '18 at 0:39













yes; essentially I have a field that I need to use to remove a whole group from the search. maybe the name 'score' in my example could be confusing; let's call it 'flag': I would like to be able to apply the multi_match only to documents that don't have that field, or, if they have it, where it is < 1. All other documents are to be ignored.

– Thomas
Nov 17 '18 at 0:42





yes; essentially I have a field that I need to use to remove a whole group from the search. maybe the name 'score' in my example could be confusing; let's call it 'flag': I would like to be able to apply the multi_match only to documents that don't have that field, or, if they have it, where it is < 1. All other documents are to be ignored.

– Thomas
Nov 17 '18 at 0:42












1 Answer
1






active

oldest

votes


















1














Assuming a POCO something like



public class MyDocument 
{
public string Tags { get; set; }

public string Title { get; set; }

public int Score { get; set; }
}


You can use the overloaded operators on queries to construct compound bool queries, to satisfy the requirements



var client = new ElasticClient(settings);

var terms = "foo bar baz";

var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
) && +(!q
.Exists(e => e
.Field(f => f.Score)
) || q
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
))
)
);


which results in the query



{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "foo bar baz",
"fuzziness": 2,
"fields": [
"tags",
"title"
]
}
}
],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "score"
}
}
]
}
},
{
"range": {
"score": {
"lt": 1.0
}
}
}
]
}
}
]
}
}
}


This is the more succinct form of the query



var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
)
)
.Filter(fi => fi
.Bool(bb => bb
.Should(sh => sh
.Bool(bbb => bbb
.MustNot(mn => mn
.Exists(e => e
.Field(f => f.Score)
)
)
), sh => sh
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
)
)
)
)
)
)
);





share|improve this answer
























  • Super helpful! thanks a lot Russ!

    – Thomas
    Nov 21 '18 at 22:02












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',
autoActivateHeartbeat: false,
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%2f53338244%2felastic-search-query-with-nest-search-for-text-but-also-filtering-by-flags%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









1














Assuming a POCO something like



public class MyDocument 
{
public string Tags { get; set; }

public string Title { get; set; }

public int Score { get; set; }
}


You can use the overloaded operators on queries to construct compound bool queries, to satisfy the requirements



var client = new ElasticClient(settings);

var terms = "foo bar baz";

var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
) && +(!q
.Exists(e => e
.Field(f => f.Score)
) || q
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
))
)
);


which results in the query



{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "foo bar baz",
"fuzziness": 2,
"fields": [
"tags",
"title"
]
}
}
],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "score"
}
}
]
}
},
{
"range": {
"score": {
"lt": 1.0
}
}
}
]
}
}
]
}
}
}


This is the more succinct form of the query



var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
)
)
.Filter(fi => fi
.Bool(bb => bb
.Should(sh => sh
.Bool(bbb => bbb
.MustNot(mn => mn
.Exists(e => e
.Field(f => f.Score)
)
)
), sh => sh
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
)
)
)
)
)
)
);





share|improve this answer
























  • Super helpful! thanks a lot Russ!

    – Thomas
    Nov 21 '18 at 22:02
















1














Assuming a POCO something like



public class MyDocument 
{
public string Tags { get; set; }

public string Title { get; set; }

public int Score { get; set; }
}


You can use the overloaded operators on queries to construct compound bool queries, to satisfy the requirements



var client = new ElasticClient(settings);

var terms = "foo bar baz";

var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
) && +(!q
.Exists(e => e
.Field(f => f.Score)
) || q
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
))
)
);


which results in the query



{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "foo bar baz",
"fuzziness": 2,
"fields": [
"tags",
"title"
]
}
}
],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "score"
}
}
]
}
},
{
"range": {
"score": {
"lt": 1.0
}
}
}
]
}
}
]
}
}
}


This is the more succinct form of the query



var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
)
)
.Filter(fi => fi
.Bool(bb => bb
.Should(sh => sh
.Bool(bbb => bbb
.MustNot(mn => mn
.Exists(e => e
.Field(f => f.Score)
)
)
), sh => sh
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
)
)
)
)
)
)
);





share|improve this answer
























  • Super helpful! thanks a lot Russ!

    – Thomas
    Nov 21 '18 at 22:02














1












1








1







Assuming a POCO something like



public class MyDocument 
{
public string Tags { get; set; }

public string Title { get; set; }

public int Score { get; set; }
}


You can use the overloaded operators on queries to construct compound bool queries, to satisfy the requirements



var client = new ElasticClient(settings);

var terms = "foo bar baz";

var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
) && +(!q
.Exists(e => e
.Field(f => f.Score)
) || q
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
))
)
);


which results in the query



{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "foo bar baz",
"fuzziness": 2,
"fields": [
"tags",
"title"
]
}
}
],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "score"
}
}
]
}
},
{
"range": {
"score": {
"lt": 1.0
}
}
}
]
}
}
]
}
}
}


This is the more succinct form of the query



var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
)
)
.Filter(fi => fi
.Bool(bb => bb
.Should(sh => sh
.Bool(bbb => bbb
.MustNot(mn => mn
.Exists(e => e
.Field(f => f.Score)
)
)
), sh => sh
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
)
)
)
)
)
)
);





share|improve this answer













Assuming a POCO something like



public class MyDocument 
{
public string Tags { get; set; }

public string Title { get; set; }

public int Score { get; set; }
}


You can use the overloaded operators on queries to construct compound bool queries, to satisfy the requirements



var client = new ElasticClient(settings);

var terms = "foo bar baz";

var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
) && +(!q
.Exists(e => e
.Field(f => f.Score)
) || q
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
))
)
);


which results in the query



{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "foo bar baz",
"fuzziness": 2,
"fields": [
"tags",
"title"
]
}
}
],
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "score"
}
}
]
}
},
{
"range": {
"score": {
"lt": 1.0
}
}
}
]
}
}
]
}
}
}


This is the more succinct form of the query



var searchResponse = client.Search<MyDocument>(s => s
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(mm => mm
.Fields(f => f
.Field(ff => ff.Tags)
.Field(ff => ff.Title)
)
.Query(terms)
.Fuzziness(Fuzziness.EditDistance(2))
)
)
.Filter(fi => fi
.Bool(bb => bb
.Should(sh => sh
.Bool(bbb => bbb
.MustNot(mn => mn
.Exists(e => e
.Field(f => f.Score)
)
)
), sh => sh
.Range(r => r
.Field(f => f.Score)
.LessThan(1)
)
)
)
)
)
)
);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 23:17









Russ CamRuss Cam

105k24170229




105k24170229













  • Super helpful! thanks a lot Russ!

    – Thomas
    Nov 21 '18 at 22:02



















  • Super helpful! thanks a lot Russ!

    – Thomas
    Nov 21 '18 at 22:02

















Super helpful! thanks a lot Russ!

– Thomas
Nov 21 '18 at 22:02





Super helpful! thanks a lot Russ!

– Thomas
Nov 21 '18 at 22:02




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338244%2felastic-search-query-with-nest-search-for-text-but-also-filtering-by-flags%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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python