简体   繁体   English

如何在Searchkit中使用SearchBox构建复合查询?

[英]How can I build a compound query with SearchBox in searchkit?

I'm using searchkit to try to build a basic text search. 我正在使用searchkit尝试构建基本的文本搜索。 I think the query I want to build is fairly simple. 我认为我要构建的查询非常简单。 It needs to be structured like this: 它需要这样的结构:

{  
  "query":{  
    "bool":{  
      "must":[
        {  
          "multi_match":{
            "query":"test search",
            "type":"phrase_prefix",
            "fields":[
              "field_1^5",
              "field_2^4",
              "field_3"
            ]
          }
        },
        {
          "term":
          {
            "field_id": "3"
          }
        }
      ],
      "must_not":[
        {
          "term":
          {
            "status": "archived"
          }
        }
      ]
    }
  },
  "size":6,
  "highlight":{  
    "fields":{  
      "field_1":{},
      "field_2":{},
      "field_3":{}
    }
  }
}

I've tried using the prefixQueryFields attribute, which gave me something fairly close to what I wanted except it was using a BoolShould rather than a BoolMust , plus it was always including the default SimpleQueryString query. 我尝试使用prefixQueryFields属性,该属性与我想要的东西非常接近,除了它使用的是BoolShould而不是BoolMust ,而且它始终包含默认的SimpleQueryString查询。 It looked something like this: 它看起来像这样:

const prefixQueryFields = [
  'field_1^5',
  'field_2^4',
  'field_3',
];

...

<SearchBox
  searchOnChange={true}
  prefixQueryFields={prefixQueryFields}
/>

I couldn't figure out the issues there easily and decided to go with the queryBuilder attribute in SearchBox . 我无法轻松找出问题所在,因此决定使用SearchBoxqueryBuilder属性。 This is what I came up with: 这是我想出的:

_queryBuilder(queryString) {
  const prefixQueryFields = [
    'field_1^5',
    'field_2^4',
    'field_3',
  ];
  return new ImmutableQuery()
    .addQuery(BoolMust([
      MultiMatchQuery(queryString, {
        type: 'phase_prefix',
        fields: prefixQueryFields,
      })
    ]))
    .addQuery(BoolMustNot([
      TermQuery('status', 'archived'),
    ]));
}

...

<SearchBox
  searchOnChange={true}
  queryBuilder={this.queryBuilder}
/>

This query came out even more messed up, and I have no idea what to try next after checking the documentation and a cursory look at the source code. 该查询的结果更加混乱,在检查文档并粗略查看源代码后,我不知道下一步该怎么做。

(For the sake of brevity, I will not bother including the incorrect queries these two attempts created unless someone thinks that info will be useful.) (为简洁起见,除非有人认为该信息会有用,否则我不会理会这两次尝试创建的错误查询。)

Figured it out. 弄清楚了。 Using the QueryDSL structures wasn't working out very well, but apparently you can create the query with pure JSON, which worked great. 使用QueryDSL结构效果不是很好,但是显然您可以使用纯JSON创建查询,效果很好。 Basically updated my query builder to return as so: 基本上更新了我的查询生成器,使其返回为:

return {
  bool: {
    must: [
      {
        multi_match:{
          query: queryString,
          type: 'phrase_prefix',
          fields: prefixQueryFields,
        }
      }
    ],
    must_not: [
      {
        term: {
          status: 'archived',
        }
      }
    ]
  }
};

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

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