美文网首页
elasticsearch三层关系最终版

elasticsearch三层关系最终版

作者: 陈文瑜 | 来源:发表于2019-08-06 16:59 被阅读0次

三层关系编码

  • 设置setting
    PUT last_tree_index
    {
      "settings": {
        "analysis": {
          "filter": {   # filter过滤器设置
            "edge_ngram_filter":{
              "type":"edge_ngram",
              "min_gram":1,
              "max_gram":50
            },
            "pinyin_simple_filter":{
              "type":"pinyin",
              "keep_first_letter":true,
              "keep_separate_first_letter":false,
              "keep_full_pinyin":false,
              "keep_original":false,
              "limit_first_letter_length":50,
              "lowercase":true
            },
            "pinyin_full_filter":{
              "type":"pinyin",
              "keep_first_letter":false,
              "keep_separate_first_letter":false,
              "keep_full_pinyin":true,
              "none_chinese_pinyin_tokenize":true,
              "keep_original":false,
              "limit_first_letter_length":50,
              "lowercase":true
            }
          },
          "tokenizer": {  # tokenizer 分词器设置
            "ik_max_word":{
              "type":"ik_max_word",
              "use_smart":true
            }
          },
          "analyzer": {  #analyzer 分析器设置
            "ngramIndexAnalyzer":{
              "type":"custom",
              "tokenizer":"keyword",
              "filter":[
                "edge_ngram_filter",
                "lowercase"
                ]
            },
            "ikIndexAnalyzer":{
              "type":"custom",
              "tokenizer":"ik_max_word"
            },
            "pinyiSimpleIndexAnalyzer":{
              "tokenizer":"keyword",
              "filter":[
                "pinyin_simple_filter",
                "edge_ngram_filter",
                "lowercase"
                ]
            },
            "pinyiFullIndexAnalyzer":{
              "tokenizer":"keyword",
              "filter":[
                "pinyin_full_filter",
                "lowercase"
                ]
            }
          }
        }
      }
    }
    
  • 设置mapping
    PUT last_tree_index/_mapping/_doc
    {
      "properties":{
        "my_join_field":{
          "type":"join",
          "relations":{
            "question":"answer",
            "answer":"vote"
          }
        },
        "fullText":{
          "type":"text",
          "analyzer":"ikIndexAnalyzer",
          "fields":{
            "ngram":{
              "type":"text",
              "analyzer":"ngramIndexAnalyzer"
            },
            "SPY":{
              "type":"text",
              "analyzer":"pinyiSimpleIndexAnalyzer"
            },
            "FPY":{
              "type":"text",
              "analyzer":"pinyiFullIndexAnalyzer"
            }
          }
        }
      }
    }
    
  • 插入数据
    PUT last_tree_index/_doc/1?refresh
    {
      "fullText":"这是一个问题",
      "my_join_field":"question"
    }
    
    PUT last_tree_index/_doc/2?refresh
    {
      "fullText":"这是另外一个问题",
      "my_join_field":"question"
    }
    
    PUT last_tree_index/_doc/3?routing=1&refresh
    {
      "fullText":"这是一个答案",
      "my_join_field":{
        "name":"answer",
        "parent":"1"
      }
    }
    
    PUT last_tree_index/_doc/4?routing=2&refresh
    {
      "fullText":"这是另外一个答案",
      "my_join_field":{
        "name":"answer",
        "parent":"2"
      }
    }
    
    PUT last_tree_index/_doc/5?routing=1&refresh
    {
      "fullText":"这是一个赞同",
      "my_join_field":{
        "name":"vote",
        "parent":"3"
      }
    }
    
    PUT last_tree_index/_doc/6?routing=2&refresh
    {
      "fullText":"这是另外一个赞同",
      "my_join_field":{
        "name":"vote",
        "parent":"4"
      }
    }
    
  • 最终简单查询语句
    GET last_tree_index/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "fullText.ngram": {
                  "query": "这是问题",
                  "boost": 5 
                }
              }
            },
            {
              "match": { 
                "fullText.SPY": {
                  "query": "zswt",
                  "boost": 1 
                }
              }
            },
            {
              "match": { 
                "fullText.FPY": {
                  "query": "zheshiwenti",
                  "boost": 0.8
                }
              }
            }
          ],
          "must": [
            {
              "has_child": {
                "type": "answer",
                "query": {
                  "has_child": {
                    "type": "vote",
                    "query": {
                      "bool": {
                        "should": [
                          {
                            "term": {
                              "fullText": "这是"
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                "inner_hits": {}
              }
            }
          ]
        }
      }
    }
    
  • 多字段匹配改写
    GET last_tree_index/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "query": "这是问题",
                "fields": ["fullText^10","fullText.ngram^5","fullText.SPY^1","fullText.FPY^0.8"]
              }
            }
          ],
          "must": [
            {
              "has_child": {
                "type": "answer",
                "query": {
                  "has_child": {
                    "type": "vote",
                    "query": {
                      "bool": {
                        "should": [
                          {
                            "term": {
                              "fullText": "这是"
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                "inner_hits": {}
              }
            }
          ]
        }
      }
    }
    

相关文章

网友评论

      本文标题:elasticsearch三层关系最终版

      本文链接:https://www.haomeiwen.com/subject/erywdctx.html