ElasticSearch Highlighting在使用Ngram Analyzer对索引进行匹配查询时失败

时间:2017-07-22 23:35:39

标签: search elasticsearch lucene full-text-search n-gram

我已经在索引和自定义_all的所有字段中创建了一个带有ngram分析器的索引。索引几个文档之后,我试图查询索引以获得类似功能的建议。

查询的输出会返回结果,但不会突出显示。

分析仪设置:

"analysis": {
  "analyzer": {
    "my_edgegram_analyzer": {
      "filter": [
        "lowercase"
      ],
      "tokenizer": "my_edge_tokenizer"
    }
  },
  "tokenizer": {
    "my_edge_tokenizer": {
      "token_chars": [
        "letter",
        "digit",
        "punctuation",
        "symbol"
      ],
      "min_gram": "3",
      "type": "ngram",
      "max_gram": "26"
    }
  }
}

映射:

{
  "st1": {
    "mappings": {
      "a": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          {
            "catch_all": {
              "match": "imp*",
              "match_mapping_type": "string",
              "mapping": {
                "analyzer": "my_edgegram_analyzer",
                "copy_to": "catch_all",
                "norms": false,
                "type": "text"
              }
            }
          }
        ],
        "properties": {
          "catch_all": {
            "type": "text",
            "store": true,
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_id": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_name": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          }
        }
      },
      "b": {
        "_all": {
          "enabled": false
        },
        "dynamic_templates": [
          {
            "catch_all": {
              "match": "imp*",
              "match_mapping_type": "string",
              "mapping": {
                "analyzer": "my_edgegram_analyzer",
                "copy_to": "catch_all",
                "norms": false,
                "type": "text"
              }
            }
          }
        ],
        "properties": {
          "catch_all": {
            "type": "text",
            "store": true,
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_id": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          },
          "imp_server_name": {
            "type": "text",
            "norms": false,
            "copy_to": [
              "catch_all"
            ],
            "analyzer": "my_edgegram_analyzer"
          }
        }
      }
    }
  }
}

文件:

http://localhost:9200/st1/b/1

{"imp_server_name":"abc1-4-jam9.my.test.com","imp_server_id":"vrock2-us"}

http://localhost:9200/st1/a/1

{"imp_server_name":"abc2-5-ajm9.my.test.com","imp_server_id":"vrock2-us"}

查询:

{
    "query": {
        "match": {
           "catch_all": {
            "query":"test",
            "analyzer": "keyword"
           }
        }

    },
"highlight": {
        "pre_tags": ["<b>"],
        "post_tags": ["</b>"],
    "fields": {
      "*": {}
    },
    "require_field_match": false
 }
}

响应:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.16292635,
    "hits": [
      {
        "_index": "st1",
        "_type": "a",
        "_id": "1",
        "_score": 0.16292635,
        "_source": {
          "imp_server_name": "abc2-5-ajm9.my.test.com",
          "imp_server_id": "vrock2-us"
        },
        "highlight": {
          "imp_server_name": [
            "abc2-5-ajm9.my.test.com"
          ],
          "catch_all": [
            "abc2-5-ajm9.my.test.com"
          ]
        }
      },
      {
        "_index": "st1",
        "_type": "b",
        "_id": "1",
        "_score": 0.16292635,
        "_source": {
          "imp_server_name": "abc1-4-jam9.my.test.com",
          "imp_server_id": "vrock2-us"
        },
        "highlight": {
          "imp_server_name": [
            "abc1-4-jam9.my.test.com"
          ],
          "catch_all": [
            "abc1-4-jam9.my.test.com"
          ]
        }
      }
    ]
  }
}

如何在上述场景中突出显示工作。以下是预期产量:

预期输出:

"highlight": {
  "imp_server_name": [
    "abc2-5-ajm9.my.<b>test</b>.com"
  ],
  "catch_all": [
    "abc2-5-ajm9.my.<b>test</b>.com"
  ]
}

1 个答案:

答案 0 :(得分:0)

我可以通过在架构中设置term_vector来获得结果。

<html>
<head>  
<style>

.container {
  position: relative;
  width: 50%;
}

.image {
  opacity: 1;
  display: block;
  width: 100%;
  height: auto;
  transition: .5s ease;
  backface-visibility: hidden;
}

.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
 -ms-transform: translate(-50%, -50%)
}

.container:hover .image {
  opacity: 0.3;
}

.container:hover .middle {
  opacity: 1;
}

.text {
  background-color: #000000;
  color: black;
  font-size: 16px;
  padding: 16px 32px;
}

.dropbtn {
  background-color: ##DCDCDC;
  color: black;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover,
.dropbtn:focus {
  background-color: ##DCDCDC;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: ##DCDCDC;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

 .dropdown-content a {
   color: black;
   padding: 12px 16px;
   text-decoration: none;
   display: block;
}

a:hover {
 text-decoration: underline;
}

.dropdown a:hover {
  background-color: ##DCDCDC
}

.show {
  display: block;
}

</style>
<body>

   <div class="container">
    <img alt="Avatar" class="image" src="https://farm5.staticflickr.com/4397/35532470254_614bf14a8b_b.jpg" style="width: 100%;" />
    <div class="middle">
      <div class="dropdown">
         <button class="dropbtn" onclick="myFunction()">Books</button>
      </div>
<div class="dropdown-content" id="myDropdown">
        <a href="http://www.mynerdydelights.com/search/label/books">All</a>
        <a href="http://www.mynerdydelights.com/search/label/five%20things%20about%20books">Five Things About</a>
        <a href="https://www.blogger.com/blogger.g?blogID=6842912760136368596#contact">Hauls</a>
      </div>
</div>
<br />
    <br />

    <body>

      <div class="container">
        <img alt="Avatar" class="image" src="https://farm5.staticflickr.com/4405/35532468424_c6ace22b6a_b.jpg" style="width: 100%;" />
        <div class="middle">
          <div class="dropdown">
            <button class="dropbtn" onclick="myFunction()">TV</button>
          </div>
<div class="dropdown-content" id="myDropdown">
            <a href="http://www.mynerdydelights.com/search/label/tv%20shows">All</a>
            <a href="http://www.mynerdydelights.com/search/label/five%20things%20about%20books">Five Things About</a>
            <a href="https://www.blogger.com/blogger.g?blogID=6842912760136368596#contact">Hauls</a>
          </div>
</div>
<html>
        <script>
          /* When the user clicks on the button, 
          toggle between hiding and showing the dropdown content */
          function myFunction() {
           document.getElementById("myDropdown").classList.toggle("show");
        }

          // Close the dropdown if the user clicks outside of it
          window.onclick = function(event) {
           if (!event.target.matches('.dropbtn')) {

              var dropdowns = document.getElementsByClassName("dropdown-
content");
              var i;
              for (i = 0; i < dropdowns.length; i++) {
               var openDropdown = dropdowns[i];
               if (openDropdown.classList.contains('show')) {
                openDropdown.classList.remove('show');
                }
              }
            }
          }

        </script>
        </html></div>
</body></div>
</body></head></html>