Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Queries/nested query #70

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ <h3 class='mb0 no-anchor'>Bodybuilder</h3>
class='regular pre-open'>
.multiMatchQuery
</a></li>

<li><a
href='#NestedQuery'
class='regular pre-open'>
.nestedQuery
</a></li>

<li><a
href='#QueriesqueryStringQuery'
Expand Down Expand Up @@ -2331,6 +2337,90 @@ <h3 class='fl m0' id='queries'>
</div>
</div>

<div class='border-bottom' id='NestedQuery'>
<div class="clearfix small pointer toggle-sibling">
<div class="py1 contain">
<a class='icon pin-right py1 dark-link caret-right'>▸</a>
<span class='code strong strong truncate'>nestedQuery(path, query, [opts])</span>
</div>
</div>
<div class="clearfix display-none toggle-target">
<section class='p2 mb2 clearfix bg-white minishadow'>



<p>Construct a Nested query.</p>


<div class='pre p1 fill-light mt0'>nestedQuery(path: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>, query: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>, opts: [<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>](default {})): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></div>










<div class='py1 quiet mt1 prose-big'>Parameters</div>
<div class='prose'>

<div class='space-bottom0'>
<div>
<span class='code bold'>path</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</code> Path to the nested object.

</div>

</div>

<div class='space-bottom0'>
<div>
<span class='code bold'>query</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code> A query for the nested object.

</div>

</div>

<div class='space-bottom0'>
<div>
<span class='code bold'>opts</span> <code class='quiet'>([<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>](default {}))</code> Only one optional field is available right now: score_mode. See
<a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html#CO273-1">https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html#CO273-1</a>.

</div>

</div>

</div>






<div class='py1 quiet mt1 prose-big'>Returns</div>
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></code>:
Nested query.














</section>


</div>
</div>

<div class='border-bottom' id='QueriesmatchQuery'>
<div class="clearfix small pointer toggle-sibling">
<div class="py1 contain">
Expand Down
2 changes: 2 additions & 0 deletions src/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import boolQuery from './bool-query'
import fuzzyQuery from './fuzzy-query'
import matchQuery from './match-query'
import multiMatchQuery from './multi-match-query'
import nestedQuery from './nested-query'
import queryStringQuery from './query-string-query'
import rangeQuery from './range-query'
import termQuery from './term-query'
Expand All @@ -26,6 +27,7 @@ export default {
match: matchQuery,
multi_match: multiMatchQuery,
multiMatch: multiMatchQuery,
nested: nestedQuery,
query_string: queryStringQuery,
'query-string': queryStringQuery,
queryString: queryStringQuery,
Expand Down
21 changes: 21 additions & 0 deletions src/queries/nested-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {assign} from 'lodash'

/**
* Construct a Nested query.
* https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html
*
* @memberof Queries
*
* @param {String} path The nested object the query is referring to. See https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html#CO272-2
* @param {Object} query Fully-formed query.
* @param {Object} opts Right now, the only optional field is score_mode. See https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-query.html#CO273-1
* @return {Object} Nested query.
*/
export default function nestedQuery(path, query, opts = {}) {
return {
nested: assign({
path: path,
query: query
}, opts)
}
}
60 changes: 60 additions & 0 deletions test/queries/nested-query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import nestedQuery from '../../src/queries/nested-query'
import {expect} from 'chai'

describe('nestedQuery', () => {

it('should create a nested query query', () => {
let query = {bool: {must: [{match: {'comments.name': 'john'}}, {match: {'comments.age': 28}}]}}
let result = nestedQuery('comments', query)
expect(result).to.eql({
nested: {
path: 'comments',
query: {
bool: {
must: [
{
match: {
'comments.name': 'john'
}
},
{
match: {
'comments.age': 28
}
}
]
}
}
}
})
})

it('should create a nested query query with score_mode', () => {
let query = {bool: {must: [{match: {'comments.name': 'john'}}, {match: {'comments.age': 28}}]}}
let opts = {score_mode: 'max'}
let result = nestedQuery('comments', query, opts)
expect(result).to.eql({
nested: {
path: 'comments',
score_mode: 'max',
query: {
bool: {
must: [
{
match: {
'comments.name': 'john'
}
},
{
match: {
'comments.age': 28
}
}
]
}
}
}
})
})

})