简体   繁体   English

在弹性搜索中使用通配符进行部分搜索

[英]Partial search using wildcard in Elastic Search

I want to search on array value in Elastic search using wildcard.我想使用通配符在弹性搜索中搜索数组值。

在此处输入图片说明

{
    "query": {
        "wildcard": {
            "short_message": {
                "value": "*nne*",
                "boost": 1.0,
                "rewrite": "constant_score"
            }
        }
    }
}

I am search on "short_messages", It's working for me.我正在搜索“short_messages”,它对我有用。 But I want to search on "messages.message" it's not working.但我想搜索“messages.message”它不起作用。

{
    "query": {
        "wildcard": {
            "messages.message": {
                "value": "*nne*",
                "boost": 1.0,
                "rewrite": "constant_score"
            }
        }
    }
}

And I also want to search for multiple fields in an array.而且我还想在数组中搜索多个字段。

For Example:- fields: ["messages.message","messages.subject", "messages.email_search"]例如:- 字段:["messages.message","messages.subject", "messages.email_search"]

It is possible then to give me the best solutions.那么就有可能给我最好的解决方案。

Thanks in Advance.提前致谢。

Seems like you are making used of nested datatype for messages .似乎您正在使用messagesnested数据类型。

You would need to make use of nested query for this:您需要为此使用nested query

POST <your_index_name>/_search
{
  "query": {
    "nested": {
      "path": "messages",
      "query": {
        "wildcard": {
          "messages.message": {
            "value": "*nne*",
            "boost": 1
          }
        }
      }
    }
  }
}

For multi-field querying, you can probably do it using query_string so basically your solution would be to make use of query_string inside a nested query .对于多字段查询,您可能可以使用query_string因此基本上您的解决方案是在nested query使用query_string

Query String:请求参数:

POST <your_index_name>/_search
{
  "query": {
    "nested": {
      "path": "messages",
      "query": {
        "query_string": {
          "fields": ["messages.message", "messages.subject"],
          "query": "*nne*",
          "boost": 1
        }
      }
    }
  }
}

Query DSL查询DSL

You can also make use of wildcard using Query DSL but then again, you need to add multiple query clauses for every field, for performance reasons I suspect that wildcard queries doesn't support multi-field querying.您还可以使用Query DSL来使用wildcard但同样,您需要为每个字段添加多个查询子句,出于性能原因,我怀疑通配符查询不支持多字段查询。

POST <your_index_name>/_search
{
  "query": {
    "nested": {
      "path": "messages",
      "query": {
        "bool": {
          "should": [
            {
              "wildcard": { 
                "messages.message": {
                  "value": "*nne*",
                  "boost": 1
                }
              }
            },
            {
              "wildcard": {
                "messages.subject": {
                  "value": "*nne*",
                  "boost": 1
                }
              }
            }
          ]
        }
      }
    }
  }
}

Note that wildcard search is not advisable because of the number of regex operations it has to do and would affect your latency to get a response, instead I would recommend you to look into Ngram Tokenizer thereby which you can make use of a simple match query to get your desired result.请注意,通配符搜索是不可取的,因为它必须执行的正则表达式操作的数量会影响您获得响应的延迟,相反,我建议您查看Ngram Tokenizer,从而您可以使用简单的匹配查询来得到你想要的结果。

Let me know if this helps!让我知道这是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM