搜索时弹性搜索排除一个字段 [英] Elasticsearch exclude one field while search

查看:88
本文介绍了搜索时弹性搜索排除一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ElasticSearch Java API进行搜索。我有四个字段:

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



  1. 名称

  2. DOB

  3. 地址

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

搜索时正在使用以下查询:

While searching I am using following query:

QueryBuilder qb = QueryBuilders.queryString( pritish );

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

。另外,我不想选择文件:

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

.fields(DOB,name,address)

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

有没有办法排除字段?我正在寻找ParitalFields。对我的情况有用吗可以有人帮忙提供Java的例子吗?

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:

/ ***************************** 创建索引 ******** ********************** /

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

/ ***************** ***** 插入数据 *************************** /

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

/ ************* 搜索查询 ********************** /

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?

推荐答案

在完全阅读映射文档后,我终于在创建映射时发现了我的错误。我正在为密钥创建映射如下(参考问题中的代码):

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

字段(index,not_analyzed)

阅读文档后,我更改了以下行:

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

字段(索引,否)

我非常感谢CodeSculptor的想法。现在,它按照我的要求工作。

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

非常感谢你。

这篇关于搜索时弹性搜索排除一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆