Cross index bool query fails


(nel.taurisson) #1

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() ) ;

}

(nel.taurisson) #2

Hi again,

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

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:

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() ) ;

   }

(Shay Banon) #3

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 :slight_smile: ).

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:

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() ) ;

}


(system) #4