简体   繁体   English

对多个字段进行弹性搜索

[英]elasticsearch upon multiple fields

I want to search a keyword in a document which has couple of fields.我想在具有几个字段的文档中搜索关键字。 the search should occur on two fields only : id and name.搜索应该只发生在两个字段上:id 和 name。

I need to write a query which has this property: If your search input(keyword) is numeric, and it happens that there are two different records which contain this keyword in id field and name field, the one with the id field should be shown first(it doesn't have to be a perfect match in the ID field).我需要编写一个具有此属性的查询:如果您的搜索输入(关键字)是数字,并且恰好有两个不同的记录在 id 字段和 name 字段中包含此关键字,则应显示带有 id 字段的记录首先(它不必是 ID 字段中的完美匹配)。

for example, if i have these two records: 1. id: 5 name:"kuku" 2. id: 10 name: "kuku523"例如,如果我有这两条记录: 1. id: 5 name:"kuku" 2. id: 10 name: "kuku523"

and i look for "5" as keyword, i should get higher score for the first record(in the hits list).我寻找“5”作为关键字,我应该为第一条记录(在命中列表中)获得更高的分数。

The "id" field is numeric, and the "name" field is a string. “id”字段是数字,“name”字段是字符串。

I've been trying to use the matchQuery, multiMatchQuery and fuzzyLikeThis.我一直在尝试使用matchQuery、multiMatchQuery 和fuzzyLikeThis。

The multiMatchQuery gives higher score to the second record instead of the first one, which looks really odd to me(because there is a "perfect match" on the first record). multiMatchQuery 给第二条记录而不是第一条记录提供更高的分数,这对我来说看起来很奇怪(因为第一条记录上有一个“完美匹配”)。

setQuery(QueryBuilders.multiMatchQuery(searchParam, ID, NAME));

and the fuzzyLike This throws an exception about the numeric field.而fuzzyLike This 会引发关于数字字段的异常。

setQuery(QueryBuilders.fuzzyLikeThisQuery(ID,NAME).likeText(searchParam))

the tremQuery doesn't really gives a solution. tremQuery 并没有真正给出解决方案。

What query i should use in a case like this?在这种情况下我应该使用什么查询?

You need to make id a String field as full-text-searches do not work on numeric fields.您需要将id设为String字段,因为全文搜索不适用于数字字段。

OR或者

If you still need id field to be numeric for term queries, you can use multifields Create a multi-field, that will contain a number field and also a string for searching如果您仍然需要id字段作为术语查询的数字,您可以使用 multifields 创建一个多字段,它将包含一个数字字段和一个用于搜索的字符串

PUT /test/items/_mapping
{
  "items" : {
    "properties" : {
      "id" : {
          "fields" : {
            "numeric" : {
              "type" : "integer",
              "index" : "not_analyzed"
            },
            "text" : {
              "type" : "string",
              "index" : "analyzed"
            }
          }
        }
      }
    }
}

Now, you can use id.numeric for numeric query and id.text for text queries现在,您可以使用id.numeric进行数字查询,使用id.text进行文本查询

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

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