如何在创建索引时向elasticsearch模式添加过滤器和映射?

时间:2018-04-17 08:51:44

标签: elasticsearch elasticsearch-5

我希望在编制索引时使用类似于同义词和停用词的过滤器以及弹性搜索模式中的映射类型。下面是我正在使用的json。但是当我使用下面的json时,我能够得到映射,但过滤器丢失了。可能是什么原因? (我正在使用elasticsearch 6.2)

nlp_settings = {
"settings": {
"index" : {
    "number_of_shards": 1,
    "analysis": {
        "analyzer": {
            "synonym": {
                "tokenizer": "standard",
                "filter": ["synonym", "stop_words", "lowercase",
                           "stop_words_user", "synonym_user"]
            }
        },
        "filter": {
            "synonym": {
                "type": "synonym",
                "synonyms_path": "synonyms.txt"
            },
            "stop_words": {
                "type": "stop",
                "stopwords_path": "stopwords.txt"
            },
            "stop_words_user": {
                "type": "stop",
                "stopwords": "_none_"
            },
            "synonym_user": {
                "type": "synonym",
                "synonyms": default_synonym
            }
        }
    }
    }
    },
    "mappings": {
        "doc": {
          "properties": {
            "section":{"type": "text"}, 
            "document_name": {"type": "text"},
            "dir_path_info": {"type": "text"},
            "nlu_raw": {
                    "noun_list": {"type": "nested"},
                    "verb_list": {"type": "nested"},
                },
            "nlu": {
                    "noun": {"type": "nested"},
                    "verb": {"type": "nested"}    
                }
            }
        }
    }
}

当我使用映射和过滤器时,我从这个URL http://localhost:9233/test/_settings获取以下JSON {{3}}

{
"test": {
    "settings": {
        "index": {
            "creation_date": "1523962921677",
            "number_of_shards": "5",
            "number_of_replicas": "1",
            "uuid": "FevdHGZjQm6ke2FgeNdnMQ",
            "version": {
                "created": "6020199"
            },
            "provided_name": "test"
        }
    }
}
}

然而,我真正想要的是

{
"test": {
    "settings": {
        "index": {
            "number_of_shards": "1",
            "provided_name": "test",
            "creation_date": "1523963029203",
            "analysis": {
                "filter": {
                    "synonym": {
                        "type": "synonym",
                        "synonyms_path": "synonyms.txt"
                    },
                    "synonym_user": {
                        "type": "synonym",
                        "synonyms": [
                            "a, a"
                        ]
                    },
                    "stop_words_user": {
                        "type": "stop",
                        "stopwords": [
                            "please",
                            "help"
                        ]
                    },
                    "stop_words": {
                        "type": "stop",
                        "stopwords_path": "stopwords.txt"
                    }
                },
                "analyzer": {
                    "synonym": {
                        "filter": [
                            "synonym",
                            "stop_words",
                            "lowercase",
                            "stop_words_user",
                            "synonym_user"
                        ],
                        "tokenizer": "standard"
                    }
                }
            },
            "number_of_replicas": "1",
            "uuid": "CiBBgngdR_aNHkY1m0EtXw",
            "version": {
                "created": "6020199"
            }
        }
    }
  }
}

当我从架构中删除映射时,我得到了这个。

1 个答案:

答案 0 :(得分:1)

settingsmappings应处于同一级别。所以:

{
  "settings": {
    "number_of_shards": 1,
    "analysis": {
      "analyzer": {
        "synonym": {
          "tokenizer": "standard",
          "filter": [
            "synonym",
            "stop_words",
            "lowercase",
            "stop_words_user",
            "synonym_user"
          ]
        }
      },
      "filter": {
        "synonym": {
          "type": "synonym",
          "synonyms_path": "synonyms.txt"
        },
        "stop_words": {
          "type": "stop",
          "stopwords_path": "stopwords.txt"
        },
        "stop_words_user": {
          "type": "stop",
          "stopwords": "_none_"
        },
        "synonym_user": {
          "type": "synonym",
          "synonyms": "default_synonym"
        }
      }
    }
  },
  "mappings": {
    "doc": {
      "properties": {
        "section": {
          "type": "text"
        },
        "document_name": {
          "type": "text"
        },
        "dir_path_info": {
          "type": "text"
        },
        "nlu_raw": {
          "properties": {
            "noun_list": {
              "type": "nested"
            },
            "verb_list": {
              "type": "nested"
            }
          }
        },
        "nlu": {
          "properties": {
            "noun": {
              "type": "nested"
            },
            "verb": {
              "type": "nested"
            }
          }
        }
      }
    }
  }
}