# Cross index bool query fails

**URL:** <https://discuss.elastic.co/t/cross-index-bool-query-fails/3890>\
**Category:** Elasticsearch\
**Created:** [February 6, 2011, 6:26am UTC](https://discuss.elastic.co/t/cross-index-bool-query-fails/3890 "2011-02-06T06:26:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nel\_taurisson](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@nel\_taurisson](https://discuss.elastic.co/u/nel_taurisson)\
**Post date:** [February 6, 2011, 6:26am UTC](https://discuss.elastic.co/t/cross-index-bool-query-fails/3890/1 "2011-02-06T06:26:01Z")

</div>

Hi,

I'm trying to execute a BoolQuery with clauses on two indices and it  
fails. Is that normal (should cross index bool query be avoided ?), a  
bug or something I'm doing wrong ?

Following a test case that shows the problem.  
Thanks a lot.  
Nel

```
public void testBoolQueryCrossIndex() {
	this.client.prepareIndex()
		.setIndex( "index1")
		.setType( "doc" )
		.setId( "1" )
		.setSource( "{" +
				"\"fieldA\":\"A\" " +
				"}" ).execute().actionGet() ;

	this.client.prepareIndex()
		.setIndex( "index2")
		.setType( "doc" )
		.setId( "1" )
		.setSource( "{" +
				"\"fieldB\":\"B\" " +
				"}" ).execute().actionGet() ;

	this.client.admin().indices().prepareRefresh( "index1" ,

```

"index2" ).execute().actionGet() ;

```
	SearchResponse searchResponse = null ;
	XContentQueryBuilder q = null ;

	/*
	 * bool criteria only on index1 -> OK
	 */
	q =

```

QueryBuilders.boolQuery().must( QueryBuilders.fieldQuery( "fieldA" ,  
"A" ) ) ;  
searchResponse = this.client.prepareSearch( "index1" , "index2" )  
.setSearchType( SearchType.DEFAULT )  
.setQuery( q )  
.execute()  
.actionGet();

```
	assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;

	/*
	 * bool criteria only on index2 -> OK
	 */
	q = QueryBuilders.boolQuery()
		.must( QueryBuilders.fieldQuery( "fieldB" , "B" ) )
		;

	searchResponse = this.client.prepareSearch( "index1" , "index2" )
		.setSearchType( SearchType.DEFAULT )
		.setQuery( q )
		.execute()
		.actionGet();

	assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;

	/*
	 * bool criteria on index1 and index2 -> FAILS
	 */
	q = QueryBuilders.boolQuery()
		.must( QueryBuilders.fieldQuery( "fieldA" , "A" ) )
		.must( QueryBuilders.fieldQuery( "fieldB" , "B" ) )
		;

	searchResponse = this.client.prepareSearch( "index1" , "index2" )
		.setSearchType( SearchType.DEFAULT )
		.setQuery( q )
		.execute()
		.actionGet();

	assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;

}
```

---

<div class="post-metadata">

**Author:** ![nel\_taurisson](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@nel\_taurisson](https://discuss.elastic.co/u/nel_taurisson)\
**Post date:** [February 6, 2011, 5:32pm UTC](https://discuss.elastic.co/t/cross-index-bool-query-fails/3890/2 "2011-02-06T17:32:28Z")

</div>

Hi again,

Thinking about it, maybe I just misunderstoog the multi index feature  
(understanding it so that it would fit my use case 🙂 ).

I thought one could store the same document in different indices so  
that each index could hold an aspect of the document, and each aspect  
could be indexed / updated seperatly. And I thought one could execute  
a query that would cross the indices boundaries.

Was I wrong ?

If I was, is there a way to implement something like "partial update"  
of a document. Here's my usecase : I have documents that are  
aggregation of independent parts (independent in terms of storage and  
update). What I'd like is that when one part gets modified there would  
be no need to re index the whole document, but only the modified part  
(so that the other part dont need to be loaded), still being able to  
search the document as a whole.

Thanks for your help.

Nel

2011/2/6 nel.taurisson [nel.taurisson@gmail.com](mailto:nel.taurisson@gmail.com):

> Hi,
> 
> I'm trying to execute a BoolQuery with clauses on two indices and it  
> fails. Is that normal (should cross index bool query be avoided ?), a  
> bug or something I'm doing wrong ?
> 
> Following a test case that shows the problem.  
> Thanks a lot.  
> Nel
> 
> ```
> public void testBoolQueryCrossIndex() {
> this.client.prepareIndex()
> .setIndex( "index1")
> .setType( "doc" )
> .setId( "1" )
> .setSource( "{" +
> "\"fieldA\":\"A\" " +
> "}" ).execute().actionGet() ;
> 
> this.client.prepareIndex()
> .setIndex( "index2")
> .setType( "doc" )
> .setId( "1" )
> .setSource( "{" +
> "\"fieldB\":\"B\" " +
> "}" ).execute().actionGet() ;
> 
> this.client.admin().indices().prepareRefresh( "index1" ,
> 
> ```
> 
> "index2" ).execute().actionGet() ;
> 
> ```
> SearchResponse searchResponse = null ;
> XContentQueryBuilder q = null ;
> 
> /*
> * bool criteria only on index1 -> OK
> */
> q =
> 
> ```
> 
> QueryBuilders.boolQuery().must( QueryBuilders.fieldQuery( "fieldA" ,  
> "A" ) ) ;  
> searchResponse = this.client.prepareSearch( "index1" , "index2" )  
> .setSearchType( SearchType.DEFAULT )  
> .setQuery( q )  
> .execute()  
> .actionGet();
> 
> ```
> assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;
> 
> /*
> * bool criteria only on index2 -> OK
> */
> q = QueryBuilders.boolQuery()
> .must( QueryBuilders.fieldQuery( "fieldB" , "B" ) )
> ;
> 
> searchResponse = this.client.prepareSearch( "index1" , "index2" )
> .setSearchType( SearchType.DEFAULT )
> .setQuery( q )
> .execute()
> .actionGet();
> 
> assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;
> 
> /*
> * bool criteria on index1 and index2 -> FAILS
> */
> q = QueryBuilders.boolQuery()
> .must( QueryBuilders.fieldQuery( "fieldA" , "A" ) )
> .must( QueryBuilders.fieldQuery( "fieldB" , "B" ) )
> ;
> 
> searchResponse = this.client.prepareSearch( "index1" , "index2" )
> .setSearchType( SearchType.DEFAULT )
> .setQuery( q )
> .execute()
> .actionGet();
> 
> assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;
> 
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 7, 2011, 6:28pm UTC](https://discuss.elastic.co/t/cross-index-bool-query-fails/3890/3 "2011-02-07T18:28:15Z")

</div>

On Sunday, February 6, 2011 at 7:32 PM, Nel Taurisson wrote:  
Hi again,

> Thinking about it, maybe I just misunderstoog the multi index feature  
> (understanding it so that it would fit my use case 🙂 ).
> 
> I thought one could store the same document in different indices so  
> that each index could hold an aspect of the document, and each aspect  
> could be indexed / updated seperatly. And I thought one could execute  
> a query that would cross the indices boundaries.
> 
> Was I wrong ?  
> Yep :). Its just another "database" that holds documents.
> 
> If I was, is there a way to implement something like "partial update"  
> of a document. Here's my usecase : I have documents that are  
> aggregation of independent parts (independent in terms of storage and  
> update). What I'd like is that when one part gets modified there would  
> be no need to re index the whole document, but only the modified part  
> (so that the other part dont need to be loaded), still being able to  
> search the document as a whole.  
> Nope, though parent child support might help you to get to something close to it.
> 
> Thanks for your help.
> 
> Nel
> 
> 2011/2/6 nel.taurisson [nel.taurisson@gmail.com](mailto:nel.taurisson@gmail.com):
> 
> > Hi,
> > 
> > I'm trying to execute a BoolQuery with clauses on two indices and it  
> > fails. Is that normal (should cross index bool query be avoided ?), a  
> > bug or something I'm doing wrong ?
> > 
> > Following a test case that shows the problem.  
> > Thanks a lot.  
> > Nel
> > 
> > public void testBoolQueryCrossIndex() {  
> > this.client.prepareIndex()  
> > .setIndex( "index1")  
> > .setType( "doc" )  
> > .setId( "1" )  
> > .setSource( "{" +  
> > ""fieldA":"A" " +  
> > "}" ).execute().actionGet() ;
> > 
> > this.client.prepareIndex()  
> > .setIndex( "index2")  
> > .setType( "doc" )  
> > .setId( "1" )  
> > .setSource( "{" +  
> > ""fieldB":"B" " +  
> > "}" ).execute().actionGet() ;
> > 
> > this.client.admin().indices().prepareRefresh( "index1" ,  
> > "index2" ).execute().actionGet() ;
> > 
> > SearchResponse searchResponse = null ;  
> > XContentQueryBuilder q = null ;
> > 
> > /\*
> > 
> > - bool criteria only on index1 -\> OK  
> > \*/  
> > q =  
> > QueryBuilders.boolQuery().must( QueryBuilders.fieldQuery( "fieldA" ,  
> > "A" ) ) ;  
> > searchResponse = this.client.prepareSearch( "index1" , "index2" )  
> > .setSearchType( SearchType.DEFAULT )  
> > .setQuery( q )  
> > .execute()  
> > .actionGet();
> > 
> > assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;
> > 
> > /\*
> > 
> > - bool criteria only on index2 -\> OK  
> > \*/  
> > q = QueryBuilders.boolQuery()  
> > .must( QueryBuilders.fieldQuery( "fieldB" , "B" ) )  
> > ;
> > 
> > searchResponse = this.client.prepareSearch( "index1" , "index2" )  
> > .setSearchType( SearchType.DEFAULT )  
> > .setQuery( q )  
> > .execute()  
> > .actionGet();
> > 
> > assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;
> > 
> > /\*
> > 
> > - bool criteria on index1 and index2 -\> FAILS  
> > \*/  
> > q = QueryBuilders.boolQuery()  
> > .must( QueryBuilders.fieldQuery( "fieldA" , "A" ) )  
> > .must( QueryBuilders.fieldQuery( "fieldB" , "B" ) )  
> > ;
> > 
> > searchResponse = this.client.prepareSearch( "index1" , "index2" )  
> > .setSearchType( SearchType.DEFAULT )  
> > .setQuery( q )  
> > .execute()  
> > .actionGet();
> > 
> > assertEquals( 1L , searchResponse.hits().getTotalHits() ) ;
> > 
> > }

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:12am UTC](https://discuss.elastic.co/t/cross-index-bool-query-fails/3890/4 "2017-07-06T04:12:25Z")

</div>

Closed on July 6, 2017, 4:12am UTC


