仅索引ElasticSearch映射中的几个字段

时间:2019-01-16 14:18:42

标签: python-3.x elasticsearch indexing searchable

我正在使用ElasticSearch 6.5。 我已经使用以下代码对一个csv文件建立了索引:

def create_index(es_object, index_name):
    created = False
    # index settings
    settings = {
        "settings": {
            "number_of_shards": 1,
            "number_of_replicas": 0,
            "analysis": {
                "filter": {
                    "dbl_metaphone": { 
                        "type":    "phonetic",
                        "encoder": "beider_morse"
                    }
                },
                "analyzer": {
                    "dbl_metaphone": {
                        "tokenizer": "standard",
                        "filter":    "beider_morse"
                    }
                }
            }
        },
        "mappings": {
            "test": {
                #"dynamic": "strict",
                "properties": {
                    "family name": {
                        "type": "text",
                        "index": "analyzed",
                        "fields": {
                            "phonetic": { 
                                "type":     "string",
                                "analyzer": "dbl_metaphone"
                            }
                        }
                    },
                    "Firstname": {
                        "type": "text",
                        "index": "analyzed",
                        "fields": {
                            "phonetic": { 
                                "type":     "string",
                                "analyzer": "dbl_metaphone"
                            }
                        }
                    },

                "Date of birth": {
                    "type": "text",
                    "index": "false"
                },  
                "Place of birth": {
                    "type": "text",
                    "index": "false",
                },

            }
        }
    }
}

    try:
        if not es_object.indices.exists(index_name):
            # Ignore 400 means to ignore "Index Already Exist" error.
            es_object.indices.create(index=index_name, ignore=400, body=settings)
            print('Created Index')
        created = True
    except Exception as ex:
        print(str(ex))
    finally:
        return created

问题是,当我尝试使用kibana搜索数据时,所有字段都是可搜索且可聚合的。我想将“出生日期”和“出生地点”从可搜索和可汇总的位置中排除。

谁能解释我的映射有什么问题以及如何更新索引以实现它?

谢谢

2 个答案:

答案 0 :(得分:0)

让我们尝试一个最小的示例(通过Kibana中的Console添加,但是您可以很容易地将其更改为普通curl命令):

PUT test
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "_doc": {
      "dynamic": "strict",
      "properties": {
        "family name": {
          "type": "text",
          "index": "true"
        },
        "Firstname": {
          "type": "text",
          "index": "true"
        },
        "Date of birth": {
          "type": "text",
          "index": "false"
        }
      }
    }
  }
}

PUT /test/_doc/1
{
  "family name": "foo",
  "Firstname": "bar",
  "Date of birth": "baz"
}

这对我有用。我可以找到foobar,但找不到baz

enter image description here

刷新索引模式后,出生日期字段既不可搜索也不可聚合:

enter image description here

一些快速观察:

  1. index is true or false。它在不同的领域,因此可能与您的问题无关。
  2. 除非您将来希望受苦,否则我将避免在字段名称中使用空格。我可以看到很多地方出了问题。

答案 1 :(得分:0)

您需要将索引分配为“ not_analyzed”

"Date of birth": { "type": "text", "index" : "not_analyzed" },