1

I am using ElasticSearch Java API for search. I have four fields:

  1. key
  2. name
  3. DOB
  4. address

While searching I am using following query:

QueryBuilder qb =QueryBuilders.queryString("Pritish");

This is including key while searching. Also, I do not want to select fileds as:

.fields("DOB", "name", "address")

Is there any way to exclude field? I was looking for ParitalFields. Is it useful for my case? Can some one help to provide java example for this?

Now, I have added mapping as follows:

/***************************** Create Index ******************************/

XContentBuilder mapping = jsonBuilder()

    .startObject()
        .startObject(indexName)
            .startObject("properties")
                .startObject("key")
                    .field("type", "string") 
                    .field("index","not_analyzed")
                    .field("store", "false")
                .endObject()
                .startObject("addr1")
                    .field("type","string")
                .endObject()
                .startObject("DOB")
                    .field("type","string")
                .endObject()
                .startObject("name")
                    .field("type","string")
                .endObject()
            .endObject()
        .endObject()
    .endObject();

client.admin()
        .indices()
        .preparePutMapping(indexName)
        .setType(indexName)
        .setSource(mapping)
        .execute()
        .actionGet();

/********************** Insert Data *****************************/

    XContentBuilder x = jsonBuilder()

        .startObject()
            .field("key", key)
            .field("addr1", "abc road")
            .field("DOB", "09092009")
            .field("name","test")

    .endObject();
bulk.add(client.prepareIndex(indexName, indexName).setSource(x));

/************* Search Query **********************/

QueryBuilder qb =QueryBuilders.queryString("*e434*");

SearchResponse response = client.prepareSearch(indexName)

    .setQuery(qb)
    .setFrom(0)
    .setSize(100)
    .execute()
    .actionGet();

SearchHit[] results = response.getHits().hits();

When, I see the index meta data, it is showing me proper mapping as follows:

mappings: {
   indexName: {
     properties: {
     addr1: {
         type: string
     }
     name: {
         type: string
     }
     key: {
         index: not_analyzed
         type: string
     }
     acct_type: {
         type: string
     }
  }
}
}

Now, when I perform search query, it is still showing me the result which search against the key.

I am not sure what's going wrong. Can anybody please help to resolve this?

3
  • 1
    Do you want to control the fields you're searching on or the fields that get returned? Commented Apr 19, 2014 at 8:37
  • Just out of curiosity, why don't you want to return the fourth field. Is it performance? Another reason? Commented Apr 19, 2014 at 19:31
  • I want to control the fields while searching. While returning I do not control any fields. Right now, my key is random UUID. and If I search abc or 123, it is searching against address and key, and give me unnecessary results. Commented Apr 20, 2014 at 22:47

2 Answers 2

3

After reading the mapping document thoroughly, I finally found my mistake while creating mapping. I was creating mapping for key as follows (refer code in Question):

field("index","not_analyzed")

After reading the document, i changed the above line as below:

field("index","no")

I am really thankful to CodeSculptor for his idea. Now, it is working as per my requirements.

Thank you so much.

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

Comments

1

From what I understand you don't want to search on this one specific field. In this case partial fields (replaced in Elastic Search 1.0.0beta1 by source filtering) are not useful for you as they are separate thing apart from query and are meant only for getting partial information from result document. It doesn't affect if search will be performed on some field.

You can only select specific fields for search using some of classes extending QueryBuilder interface and what is a bad news for you, only possibility of selecting fields is by adding them.

On the other hand, if it is not the case where you don't want to search on this specific field in one specific case, but it's just a field that is going to contain relevant information as result and will never be searched on, you can specify this field as not indexed. You can set field's index attribute to no while mapping. This should result in having field that will be stored and returned with whole JSON as document's source, but not being searchable.

1 Comment

Hey, After reading mapping document, I think this will surely helpful for my scenario. I am using Java API to create index. Do you have any idea how to set mapping? My code looks as follows: BulkRequestBuilder bulk = client.prepareBulk(); bulk.add(client.prepareIndex(this.indexName, this.indexName, key).setSource( jsonBuilder() .startObject() .field("key", key) .endObject())); Here, I want to add mapping against key.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.