使用搜索文本的差异大小在弹性搜索中的最佳字段中搜索

时间:2018-12-05 16:39:44

标签: elasticsearch

假设我们有以下查询:

GET /_search
{
  "query": {
    "multi_match" : {
      "query":      "brown fox",
      "type":       "best_fields",
      "fields":     [ "subject", "message" ],
      "tie_breaker": 0.3
    }
  }
}

其执行方式为:

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "subject": "brown fox" }},
        { "match": { "message": "brown fox" }}
      ],
      "tie_breaker": 0.3
    }
  }
}

假定如果文本的长度仅为5,则“消息”字段可以搜索。

我该如何执行与执行相同的逻辑:

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "subject": "brown fox" }},
        { "match": { "message": "brown" }}
      ],
      "tie_breaker": 0.3
    }
  }
}

该示例取自here

1 个答案:

答案 0 :(得分:0)

因此,如果您尝试对“消息”进行完全匹配(由于棕色为5个字母,则可以阅读以下内容: https://www.elastic.co/guide/en/elasticsearch/guide/master/_finding_exact_values.html#_finding_exact_values

如果您想在检查长度时与您想要的任何分析器进行匹配,则可以使用一个分析器来进行字符串的长度,并使用另一个分析器来进行匹配。

`

ERROR - PassThroughHttpSender Failed to submit the response
org.apache.axis2.AxisFault: Payload could not be written as JSON.
        at org.apache.synapse.commons.json.JsonUtil.writeAsJson(JsonUtil.java:235)
        at org.apache.synapse.commons.json.JsonStreamFormatter.writeTo(JsonStreamFormatter.java:61)
        at org.apache.synapse.transport.passthru.PassThroughHttpSender.submitResponse(PassThroughHttpSender.java:556)
        at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:265)
        at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
        at org.apache.synapse.core.axis2.Axis2Sender.sendBack(Axis2Sender.java:212)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.send(Axis2SynapseEnvironment.java:444)
        at org.apache.synapse.mediators.builtin.SendMediator.mediate(SendMediator.java:102)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:89)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:56)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:149)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:297)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:528)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:172)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:247)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)

ERROR - Axis2Sender **Authorization-Token:{A long string having '-', '_', '.'init}**,Cache-Control:no-cache,Content-Type:application/json,Expires:-1,Pragma:no-cache,X-AspNet-Version:4.0.30319,X-Powered-By:ASP.NET,<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body/></soapenv:Envelope> Unexpected error sending message back
org.apache.axis2.AxisFault: Failed to submit the response
        at org.apache.synapse.transport.passthru.PassThroughHttpSender.handleException(PassThroughHttpSender.java:614)
        at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:267)
        at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
        at org.apache.synapse.core.axis2.Axis2Sender.sendBack(Axis2Sender.java:212)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.send(Axis2SynapseEnvironment.java:444)
        at org.apache.synapse.mediators.builtin.SendMediator.mediate(SendMediator.java:102)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:89)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:56)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:149)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:297)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:528)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:172)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:247)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)
Caused by: org.apache.axis2.AxisFault: Payload could not be written as JSON.
        at org.apache.synapse.commons.json.JsonUtil.writeAsJson(JsonUtil.java:235)
        at org.apache.synapse.commons.json.JsonStreamFormatter.writeTo(JsonStreamFormatter.java:61)
        at org.apache.synapse.transport.passthru.PassThroughHttpSender.submitResponse(PassThroughHttpSender.java:556)
        at org.apache.synapse.transport.passthru.PassThroughHttpSender.invoke(PassThroughHttpSender.java:265)

`

然后,您应该可以使用and条件进行匹配:

`

PUT ...index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "character_analyzer": {
          "type": "custom",
          "tokenizer": "character_tokenizer"
        }
      },
      "tokenizer": {
        "character_tokenizer": {
          "type": "nGram",
          "min_gram": 1,
          "max_gram": 1
        }
      }
    }
  }, 
  "mappings": {
    "<object>": {
      "properties": {
        "message": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            },
            "length": { 
              "type": "token_count",
              "analyzer": "character_analyzer"
            }
          }
        }
      }
    }
  }
}

`

最后,您也可以使用脚本而不是添加另一个分析器,如本文所述: Kibana querying for string length

`

GET /_search
{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "subject": "brown fox" }},
        { "must" : [
           { "term" : { "message.length" : 5 }},
           { "term" : { "message.keyword" : "brown" }}
        ]}
      ],
      "tie_breaker": 0.3
    }
  }
}

`